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;
}
|
(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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!