|
|
|
|
|
by _kst_
1137 days ago
|
|
`(char)'c'` is an expression of type char. Its size is one byte -- but the size of an expression isn't really relevant, since it's (conceptually) not stored in memory. You can assign the value of an expression to an object, and that object's size depends on its declared type, not on the value assigned to it. The cast is very probably not necessary. char c1 = 'c'; // The object is one byte; 'c' is converted from int to char
int c2 = 'c'; // The object is typically 4 bytes (sizeof (int))
The fact that character constants are of type int is admittedly confusing -- but given the number of contexts in which implicit conversions are applied, it rarely matters. If you assign the value 'c' to an object of type char, there is conceptually an implicit conversion from int to char, but the generated code is likely to just use a 1-byte move operation. |
|