ProgrammingHardGATE CS

What is printed? Assume int is 32 bits and unsigned char is 8 bits.

What is printed? Assume int is 32 bits and unsigned char is 8 bits.
#include <stdio.h>
int main(void) {
    unsigned char c = 0x0F;
    printf("%d %d %d", ~c, (unsigned char)~c, ~c & 0xFF);
    return 0;
}
  1. A. 240 240 240
  2. B. -16 240 15
  3. C. -16 240 240
  4. D. -16 -16 240

Correct answer: C. -16 240 240

c is promoted to int (15) before ~, so ~c is -16 (all high bits set). Converting back to unsigned char keeps the low 8 bits, 0xF0 = 240, and masking with 0xFF also gives 240.

More C Programming questions

Exams that ask this topic