c - Understanding printf? -


i have seen many question realting still have problem or misunderstanding of things. standards says: section 7.21.6/9 says

"if conversion specification invalid, behavior undefined. if argument not correct type corresponding conversion specification, behavior undefined.".

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

i know perfect format must printf("%hhd",c); or printf("%c",c);

but want know happens ?

in cases promotion occurs or down ?

is same scanf ?

no, example not undefined behaviour. char int 1 of default argument promotions. spec, 6.5.2.2 function calls, paragraph 6:

the integer promotions performed on each argument, , arguments have type float promoted double

the integer promotions (from 6.3.1.1 boolean, characters, , integers, paragraph 2):

if int can represent values of original type (as restricted width, bit-field), value converted int; otherwise, converted unsigned int. these called integer promotions. other types unchanged integer promotions.


Comments