+ 2
Need a little explanation regarding loops in c
i came across this program.. #include<stdio.h> int main() { int i = 0; for (printf("1st "); i < 2 && printf("2nd "); ++i && printf("3rd ")) { printf("* "); } return 0; } and output is 1st 2nd * 3rd 2nd * 3rd amm... why???
1 ответ
+ 1
Apart from printing the parameters, the printf function returns the number of characters successfully printed. so the condition along with i < 2 always evaluates to true because it returns 3, a non zero value considered as true in c.
So first the initialization in the for loop run and prints "1st"
then as part of condition evaluation, "2nd" gets printed.
body of the loop * is printed
then along with ++i, "3rd" gets printed, and the loop repeats for 2 times.