Casts in C do not reinterpret anything. They are value conversions. (int)c
means take the value of c
and convert it to int
, which is a no-op on essentially all systems. (The only way it could fail to be a no-op is if the range of char
is larger than the range of int
, for example if char
and int
are both 32-bit but char
is unsigned.)
If you want to reinterpret the representation (bit pattern) underlying a value, that value must first exist as an object (lvalue), not just the value of an expression (typically called "rvalue" though this language is not used in the C standard). Then you can do something like:
*(new_type *)&object;
However, except in the case where new_type
is a character type, this invokes undefined behavior by violating the aliasing rules. C++ has a sort of "reinterpret cast" to do this which can presumably avoid breaking aliasing rules, but as I'm not familiar with C++, I can't provide you with good details on it.
In your C++ example, the reason you get different results is operator overloading. (int)'A'
does not change the value or how it's interpreted; rather, the expression having a different type causes a different overload of the operator<<
function to be called. In C, on the other hand, (int)'A'
is always a no-op, because 'A'
has type int
to begin with in C.