0

Confused, Why does this work when I compile, when hold is an integer?

char value = 'p';
int hold = value;
printf("%c", hold);
ekeith
  • 644
  • 1
  • 10
  • 33
  • Do some researches about ASCII :) – Gam May 26 '17 at 02:00
  • `%c` of `printf` requires `int`. See [printf](http://en.cppreference.com/w/c/io/fprintf) – BLUEPIXY May 26 '17 at 02:01
  • 1
    Since `'p'` is an integer, you really should be asking why `char value = 'p'` works! – William Pursell May 26 '17 at 02:03
  • still compiles with no warnings @iehrlich – ekeith May 26 '17 at 02:03
  • ASCII value is 112 when I use %d, that I understand. But why does the compiler allow me store a character in an int and print it out as one? @Gam – ekeith May 26 '17 at 02:03
  • An integer can store 4 or 8 bytes (depends on the OS), while a char is always 1 byte. So technically you can store a char into an int. Even if it's a char, in the memory it's just a numer (in binary). If you call printf( ) with the %c format, it means "please printf( ) print me the corresponding symbol of this number, with ASCII" – Gam May 26 '17 at 02:14
  • @Gam-- note that the C Standard does not specify that the character encoding be ASCII. – ad absurdum May 26 '17 at 02:17
  • @DavidBowling That's right :) – Gam May 26 '17 at 02:26
  • `'p'` is an `int` in C, not `char` [Why are C character literals ints instead of chars?](https://stackoverflow.com/q/433895/995714) – phuclv May 26 '17 at 02:53

1 Answers1

3

First, in

int hold = value;

is performed implicit conversion from char to int.

Second, in

printf("%c", hold);

the %c specifier means something as convert it to char and print it as symbol.

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • The `%c` specifier tells `printf()` to _expect_ an argument of type `char`; no conversion is made (outside or the default argument promotions), and mismatched conversion specifiers and arguments cause undefined behavior. It is worth noting that `char` is an integer type. – ad absurdum May 26 '17 at 02:15
  • 2
    @DavidBowling `%c` expect an argument of type `int`. – BLUEPIXY May 26 '17 at 02:17
  • @BLUEPIXY-- oops, you are correct; I haven't looked at this part of the Standard in a while, but just now I see that here an `int` is in fact converted to `unsigned char`. – ad absurdum May 26 '17 at 02:21
  • 3
    Yes 7.21.6.1 The fprintf function p8 **c** If no l length modifier is present, the **int argument is converted to an unsigned char**, and the resulting character is written. – BLUEPIXY May 26 '17 at 02:23
  • 1
    "means something as convert it to char" is _close_. Better to incorporate [comment](https://stackoverflow.com/questions/44192417/storing-character-in-an-integer-variable#comment75399088_44192513) – chux - Reinstate Monica May 26 '17 at 02:45
  • 1
    When you call a function with an ellipsis at the end of its prototype (such as `print()`), all the arguments corresponding to the ellipsis undergo default promotions, so `float` values are converted to `double`, and types shorter than `int` are converted to `int`. The wording in the standard is complex, but it means `char` is converted to `int` automatically as `printf()` is called. That's why `printf()`'s specification says that `%c` prints an `int` converted to `unsigned char` as the character. – Jonathan Leffler May 26 '17 at 04:51