Discussion
What will be the output of the following C code?
#include <stdio.h>
void main()
{
int a = 5, b = -7, c = 0, d;
d = ++a && ++b || ++c;
printf("\n%d%d%d%d", a, b, c, d);
}
Answer: D
Correct answer: 6 -6 0 1
Work left to right with the short-circuit rules. ++a makes a 6, which is non-zero and therefore true; ++b makes b −6, also non-zero and true, so the && yields 1.
The || then has a true left operand, so it short-circuits: ++c is never evaluated and c stays 0. The result d is 1. The trap is −6, which is true because it is non-zero - truth in C is non-zero, not positive.
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