Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 510
lee77 Avatar asked Oct 18 '13 08:10

lee77


People also ask

Can char be promoted to int?

signed char or signed short can be converted to int; unsigned char or unsigned short can be converted to int if it can hold its entire value range, and unsigned int otherwise; char can be converted to int or unsigned int depending on the underlying type: signed char or unsigned char (see above);

What is integral promotion?

Some data types like char , short int take less number of bytes than int, these data types are automatically promoted to int or unsigned int when an operation is performed on them. This is called integer promotion.

Is short promoted to int?

There are some data types which take less number of bytes than integer datatype such as char, short etc. If any operations are performed on them, they automatically get promoted to int.

What is type promotion in C?

Introduction. Type promotion in C is a method to convert any variable from one datatype to another. C allows variables of different datatypes to be present in a single expression. There are different types of type conversions available in C. They are Implicit type conversion and Explicit type conversion.


1 Answers

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.

like image 88
Eric Postpischil Avatar answered Nov 15 '22 17:11

Eric Postpischil