Search code examples
cprintfundefined-behaviorvariadic-functionsinteger-promotion

Which integral promotions do take place when printing a char?


I recently read that

unsigned char x=1;
printf("%u",x);

invokes undefined behaviour since due to the format specifier %u, printf expects an unsigned int. But still I would like to understand what is going on in this example.

I think that the integral promotion rules apply with the expression printf("%u",x) and the value represented by x.

A.6.1 Integral Promotion

A character, a short integer, or an integer bit-field, all either signed or not, or an object of enumeration type, may be used in an expression wherever an integer may be used. If an int can represent all the values of the original type, then the value is converted to int; otherwise the value is converted to unsigned int. This process is called integral promotion.

What does "may be used" mean here? Does it mean 'is syntactically correct' or 'is defined behaviour'?

And how is x promoted in this example? I have read that it is promoted to an int, but if printf("%u", (int x)) is still undefined behaviour then I don't really understand why...


Solution

  • Since printf uses a variable argument list, the integer promotions are applied to its integer arguments. In any normal C implementation, the integer promotions convert an unsigned char to an int. Then you are formatting an int With a specifier for unsigned int, so the behavior is undefined.

    There is no conflict between saying that a character may be used where an integer may be used and the fact that your statement has behavior not defined by the C standard. Although you may use a character in place of an integer, the rules about what may be printed with %u still apply. If using a character results in an integer appropriate for the specifier, the behavior is defined. If using a character results in an integer inappropriate for the specifier, the behavior is not defined by the C standard.

    Discussion elsewhere on Stack Overflow concluded that an exotic C implementation might in theory conform to the C standard while having char types (plain, signed, and unsigned) as wide as int types. In such an implementation, and int could not represent all values of an unsigned char, so an unsigned char would have to be promoted to an unsigned int. However, such an implementation would be exotic and troublesome (notably with handling EOF), and you may ignore it in practice.