Discussion
What will be the output of the program?
#include <stdio.h>
int main()
{
int x=1, y=1;
for(; y; printf("%d %d\n", x, y))
{
y = x++ <= 5;
}
printf("\n");
return 0;
}
Answer: A
Correct answer: 2 1 3 1 4 1 5 1 6 1 7 0
The third clause of the for is the printf, so the values are printed after each pass through the body. The body sets y to the result of x++ <= 5, which is 1 while the old x is at most 5. So the pairs printed are 2 1, 3 1, 4 1, 5 1, 6 1 - and on the pass where x is 6 the comparison fails, giving 7 0, after which the condition y is false and the loop ends.
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