What is the "@" inside the Solve function printf for

Asked

Viewed 163 times

6

#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;
}

1 answer

6

In C, @is an int. But, the 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

Logo, you can add with an integer. In your example:

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

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

  • I think the direct answer to the question should come at the beginning. All its editions gave a deeper answer, but ended up distancing the direct content from the doubt of the focus.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.