For what to use the "@" inside the printf of the solve function

5
#include <stdio.h>
#include <stdlib.h>

void solve(int QTD_DISCOS, int origem, int destino, int temp)
{
  static int rank = 0;

  if (QTD_DISCOS > 0)
  {
    solve(QTD_DISCOS-1, origem, temp, destino);
    printf("%4d ) %c --> %c\n", ++rank, '@' + origem, '@' + destino);
    solve(QTD_DISCOS-1, temp, destino, origem);
  }
}

int main()
{
  int d;
  printf("Digite o numero de discos da Torre de Hanoi: \n");
  scanf("%d", &d);
  printf("A solucao de Hanoi eh: \n");
  solve(d, 1, 3, 2);
  return 0;
}
    
asked by anonymous 01.06.2017 / 01:32

1 answer

6

In C, '@' is an int. But, type char is the size of 1 byte.

main()
{
  char arroba = '@';

  printf("tamanho '@'  = %d\n", sizeof('@'));
  printf("tamanho char = %d\n", sizeof(arroba));

  return 0;
}

The output of the program is:

leandro@macbook /tmp % ./a.out
tamanho '@'  = 4
tamanho char = 1

So you can add with an integer. In your example:

'@' + 1 = 'A'
'@' + 2 = 'B'
'@' + 3 = 'C'

In printf, you can format it using %c , but see that you can truncate the result of the sum. In the sample program, there is no such problem.

    
01.06.2017 / 01:57