4

I was practicing my C skills online. And I got a question like:

What is the output of this program?

void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}

The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?

Noob
  • 318
  • 1
  • 3
  • 11
  • Possible duplicate of [Undefined behavior and sequence points](https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points) – Ken White Nov 22 '18 at 04:47
  • The proper declarations for `main` are `int main (void)` and `int main (int argc, char **argv)` (which you will see written with the equivalent `char *argv[]`). **note:** `main` is a function of `type int` and it returns a value. See: [C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [See What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Nov 22 '18 at 04:51
  • @DavidC.Rankin What makes you think the OP is using a hosted system compiler? See [What should main() return in C and C++?](https://stackoverflow.com/a/31263079/584518) – Lundin Nov 22 '18 at 07:44
  • Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all. – Gerhardh Nov 22 '18 at 08:24
  • @Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/)? – David C. Rankin Nov 22 '18 at 18:29
  • @DavidC.Rankin Then you can read the title of chapter 5.1.2.2. – Lundin Nov 23 '18 at 07:43
  • So you are just covering the case of the 5.1.2.1 - being everything else and implementation defined. I get what you are saying, and there are more and more embedded systems out there (now how many of them chose a `void main()` implementation -- no telling) – David C. Rankin Nov 23 '18 at 07:48

3 Answers3

8

This is explained by the sequence of events:

i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5

Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.

Havenard
  • 27,022
  • 5
  • 36
  • 62
4

i is incremented four times before the for loop exits.

  • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.
  • Once in the increment (formally iteration_expression)
  • Once more in the body (formally loop_statement) of the for loop.

As the initial value of i is 1, this brings its value to 5.

Note:
- cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.
- iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.

P.W
  • 26,289
  • 6
  • 39
  • 76
2

As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?

How can i never be less than or equal to 1 when it's initialised with a value of 1?

Did you intend to pre-increment i?

That would be:

for (i=1; ++1 <= 1; i++)

But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.

If you do

for (i=1; ++i <= 1; i++)

i will be > 1 on the first iteration and nothing will happen.

Nunchy
  • 948
  • 5
  • 11