On a platform where plain char is signed, char c = 200; is followed by int i = c; what is i?
Correct answer
C. -56, because 200 does not fit in a signed 8-bit char and wraps to 200 minus 256
14 views
C. -56, because 200 does not fit in a signed 8-bit char and wraps to 200 minus 256
14 views
Signed char range is -128 to 127, so converting 200 to signed char is implementation-defined and typically wraps to -56, and the promotion to int preserves that negative value. Using unsigned char or the uint8_t typedef keeps the value at 200 and is the correct choice for raw byte data. This is why byte buffers should not be plain char when values above 127 matter.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.