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

Discussion

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int a = 2;
        if (a >> 1)
           printf("%d\n", a);
    }

 

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

Answer: B

Correct answer: 2

a >> 1 computes a value; it does not modify a. Only the compound form a >>= 1 would assign back.

So the test evaluates \( 2 \gg 1 = 1 \), which is non-zero and therefore true, and the printf then prints a - still 2, exactly as it was declared. The distractor 1 is the value of the shift expression, which is tested but never stored, and "No Output" would require the test to be false.

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

Post your comment

Please log in or register to post a comment.