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

Discussion

What is the output of this C code?   

 int main()
    {
        if (7 & 8)
        printf("Honesty");
            if ((~7 & 0x000f) == 8)
                printf("is the best policy\n");
    }

 

  • A.Honesty is the best policy
  • B.Honesty
  • C.is the best policy
  • D.No output

Answer: C

Correct answer: is the best policy

Take the two conditions separately.

  • 7 & 8: \(0111 \;\&\; 1000 = 0000\). They share no set bit, so the value is 0 and Honesty is not printed.
  • ~7 & 0x000f: \(\sim 0111 = \ldots 11111000\); masking with \(1111\) leaves \(1000 = 8\), which does equal 8.

So only the second printf runs, and the output is is the best policy.

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

Post your comment

Please log in or register to post a comment.