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

Discussion

What is the output of this C code?  

int main()
    {
        int x = -2;
        if (!0 == 1)
            printf("yes\n");
        else
            printf("no\n");
    }

 

  • A.yes
  • B.no
  • C.Run time error
  • D.Undefined

Answer: A

Correct answer: yes

! is logical negation: it maps zero to 1 and any non-zero value to 0. So !0 is exactly 1, the comparison !0 == 1 holds, and the program prints yes. Contrast ~0, which flips bits and gives -1.

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

Post your comment

Please log in or register to post a comment.