Machine-translated by Google. Questions are authored in English.
Top

Discussion

What is the output of this C code?    

int main()
    {
        unsigned int a = 10;
        a = ~a;
        printf("%d\n", a);
    }

 

  • A.-9
  • B.-10
  • C.-11
  • D.10

Answer: C

Correct answer: -11

~ flips every bit, and for a two’s-complement integer that is exactly \(\sim a = -a - 1\):

\[ \sim 10 = -10 - 1 = -11 \]

The variable is unsigned, so it actually holds 4294967285, but %d reads those bits back as a signed int and prints -11.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.