Output of following C program?
#include<stdio.h> int main() { int i = 0; for (printf("1st\n"); i < 2 && printf("2nd\n"); ++i && printf("3rd\n")) { printf("*\n"); } return 0; } |
chevron_right
filter_none
(A) 1st
2nd
*
3rd
2nd
*
(B) 1st
2nd
*
3rd
2nd
*
3rd
(C) 1st
2nd
3rd
*
2nd
3rd
(D) 1st
2nd
3rd
*
1st
2nd
3rd
Answer: (B)
Explanation: It is just one by one execution of statements in for loop.
a) The initial statement is executed only once.
b) The second condition is printed before ‘*’ is printed. The second statement also has short circuiting logical && operator which prints the second part only if ‘i’ is smaller than 2
b) The third statement is printed after ‘*’ is printed. This also has short circuiting logical && operator which prints the second part only if ‘++i’ is not zero.
Recommended Posts:
- C | Loops & Control Structure | Question 6
- C | Loops & Control Structure | Question 4
- C | Loops & Control Structure | Question 3
- C | Loops & Control Structure | Question 12
- C | Loops & Control Structure | Question 18
- C | Loops & Control Structure | Question 13
- C | Loops & Control Structure | Question 21
- C | Loops & Control Structure | Question 14
- C | Loops & Control Structure | Question 20
- C | Loops & Control Structure | Question 19
- C | Loops & Control Structure | Question 2
- C | Loops & Control Structure | Question 17
- C | Loops & Control Structure | Question 15
- C | Loops & Control Structure | Question 5
- C | Loops & Control Structure | Question 1



