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

Discussion

What will be the output of the program?

#define P printf("%d\n", -1^~0);
#define M(P) int main()\
             {\
                P\
                return 0;\
             }
M(P)

 

  • A.1
  • B.-1
  • C.2
  • D.0

Answer: D

Correct answer: 0

The macro machinery is a distraction: M(P) expands to a main containing the printf, so the whole program reduces to printing -1 ^ ~0.

Now the arithmetic. ~0 inverts every bit of zero, giving all ones, which as a signed int is \( -1 \). So the expression is -1 ^ -1, and XOR of any value with itself is zero, since no bit position differs from itself. The answer is 0 regardless of the width of an int, which is what makes the identity \( x \mathbin{\hat{}} x = 0 \) worth knowing.

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

Post your comment

Please log in or register to post a comment.