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

Discussion

What is the output of this C code?   

 int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }

 

  • A.1
  • B.2
  • C.No output

Answer: C

Correct answer: 2

a >> 1 shifts 2 (binary 10) right by one place, giving 1. That is non-zero, so the condition is true and the body runs, printing the value of a - which the shift never changed, since >> yields a new value rather than modifying its operand. The output is 2.

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

Post your comment

Please log in or register to post a comment.