-
-
Save Codeplaza/7907345 to your computer and use it in GitHub Desktop.
| #include<stdio.h> | |
| int main() | |
| { | |
| int m, n; | |
| int i = 0, j = 4; | |
| for(m = 0, n = 0; m < i, n < j; m++, n++) | |
| { | |
| printf("Codeplaza\nCodeplaza\n"); | |
| } | |
| return 0; | |
| } |
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
The code will print "Codeplaza" 8 times (each on a separate line). The for loop executes 4 times because n is initialized to zero and j equals to 4, and at each iteration n is incremented by one. The value of m and i doesn't matter at all, because the loop guard expression is a comma expression, that by the language rules evaluates all the operands (i.e. the expressions "m < i" and "n < j"), but results in the value of the rightmost one (thus always resulting to the value of "n < j").
Same reasoning for the second example code. The value of m is assigned to be the value of the rightmost operand of the comma expression, so it will be 15 (printf returns the number of characters written). But both the expressions will be evaluated, so the program output will be "What's up programmers ? 15"
Program 1:
Output:
Explanation: no output will be shown because the command printf is inside the for loop. The for loop will execute only if m<i and n<j and at first m=i=0. That's why there will be blank output.