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

Discussion

What will be the output of the following program?
 

#include <stdio.h>
int main()
{
int num = 0, z = 3;
if ( ! (num <= 0) || ++z )
printf( "%d %d ", ++num + z++, ++z );
else
printf( "%d %d", --num + z--, --z);
return 0;
}

 

  • A.- 2 1
  • B.6 5
  • C.4 5
  • D.5 5

Answer: B

Correct answer: 6 5

num is 0, so num <= 0 is true and !(num <= 0) is 0. The || must therefore evaluate its right operand: ++z takes z to 4 and is non-zero, so the if branch runs.

In printf("%d %d ", ++num + z++, ++z) the arguments are evaluated right to left on the usual compilers: ++z makes z 5 and yields 5; then ++num gives 1 and z++ yields 5, so the first argument is 6. Output: 6 5. Note that the order of argument evaluation is unspecified, so this particular pairing is compiler-dependent.

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

Post your comment

Please log in or register to post a comment.