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

Discussion

What is the output of this C code?    

int main()
    {
        int y = 0;
        if (1 |(y = 1))
            printf("y is %d\n", y);
        else
            printf("%d\n", y);
 
    }

 

  • A.1
  • B.Run time error
  • C.Undefined

Answer: A

Correct answer: 1

| is the bitwise OR, and unlike || it does not short-circuit - both operands are always evaluated. So y = 1 runs, and the condition 1 | 1 is 1, which is true. The if branch prints y is 1.

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

Post your comment

Please log in or register to post a comment.