printf()
doesn't know the types, that's why you have to give a correct format string. The prototype for printf()
looks like this:
int printf(const char * restrict format, ...);
So, the only argument with a known type is the first one, the format string.
This also means that any argument passed after that is subject to default argument promotion -- strongly simplified, read it as any integer will be converted to at least int
-- or ask google about the term to learn each and every detail ;)
In your example, you have implementation defined behavior:
char a = 130;
If your char
could represent 130
, that's what you would see in the output of printf()
. Promoting the value to int
doesn't change the value. You're getting a negative number instead, which means 130
overflowed your char
. The result of overflowing a signed integer type during conversion in C is implementation defined, the value you're getting probably means that on you machine, char
has 8 bits (so the maximum value is 127
) and the signed integer overflow resulted in a wraparound to the negative value range. You can't rely on that behavior!
In short, the negative number is created in this line -- 130
is of type int
, assigning it to char
converts it and this conversion overflows.
Once your char
has the value -126
, passing it to printf()
just converts it to int
, not changing the value.