-1

I've never needed to use unsigned char before, but I've recently became curious about their ability to store integers. Through a little bit of reading I have done I've found that the cast from int to unsigned char is done implicitly, but I have also explicitly added the cast. Either way, the code below does not work and either prints out the alphabet letter equivalent or a small empty box.

printf("%s", "Number: ");
int input;
scanf("%d", &input);  
unsigned char num = input;
printf("%c%s%d", num, " = ", input);
return (EXIT_SUCCESS);
Derek
  • 11
  • 4

3 Answers3

1

You are printing out the number as a character (%c) instead of an integer (%hhu).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

In printf, the %c means to print the character symbol whose code is the corresponding argument.

To print the numeric value use %d instead.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

It prints a letter because you set printf() to print num's value as a character with "%c". If you use "%d" for example you can print its numeric value.

LeonMarchetti
  • 105
  • 3
  • 9