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;
}
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.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development