Skip to content

Instantly share code, notes, and snippets.

@Codeplaza
Created December 11, 2013 09:17
Show Gist options
  • Select an option

  • Save Codeplaza/7907345 to your computer and use it in GitHub Desktop.

Select an option

Save Codeplaza/7907345 to your computer and use it in GitHub Desktop.
Quiz 3.0 1
#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;
}
@chanakyamandava
Copy link

Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza
Codeplaza

@nicola-gigante
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment