Julio Batista Julio Batista - 6 months ago 60
C Question

Understanding printf?

I have seen many question realting that but I still have a problem or misunderstanding of things. The standards says:
section 7.21.6/9 says

"If a conversion specification is invalid, the behavior is undefined.
If any argument is not the correct type for the corresponding
conversion specification, the behavior is undefined.".

char c='a';
printf("%d",c); // is this undefined behavior

I know that the perfect format must be

but I want to know what happens exactly ?

In which cases the promotion occurs exactly up or down ?

Is it same for


No, your example is not undefined behaviour. char to int is one of the default argument promotions. From the spec, Function calls, paragraph 6:

the integer promotions are performed on each argument, and arguments that have type float are promoted to double

The integer promotions are (from Boolean, characters, and integers, paragraph 2):

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.