0

I wonder why exactly my output is 1 through 10 on the console.

#include <stdio.h>
#include <stdlib.h>

int main(void){
  int i = 0;
    for (;i++ <10;) 
  {
          printf("%d\n", i);
  }
}

Procedure

  1. 1st iteration: compare 0 < 10; fulfilled and print 1 why not 0?
  2. 2nd iteration: compare 1 < 10; fulfilled and print 2
  3. ... middle iterations
  4. 10th compare 9 < 10; fulfilled and print 10 why not 9?
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Jonathan Komar
  • 2,678
  • 4
  • 32
  • 43
  • 5
    What does `i++` do? – Oliver Charlesworth Nov 26 '17 at 14:16
  • 1
    Can you explain very clearly, your assumptions about this operator and for loops, that lead you to expect item #1 to print 0? By editing the question please. Not in the comments. – Mad Physicist Nov 26 '17 at 14:17
  • *Why* would you want to write code like that? When you have to post a question about what the code does, there's something wrong. Just because you *can* write confusing code doesn't make it *smart* code. Clever code is **BAD** code. – Andrew Henle Nov 26 '17 at 14:22
  • `for(;i<10;) { printf("%d\n", ++i); }` would give the same thing. – Déjà vu Nov 26 '17 at 14:23
  • 1
    @Andrew. This looks like a fundamental misunderstanding of language constructs, not an attempted cleverness. Could be both. – Mad Physicist Nov 26 '17 at 14:24
  • This is a purely knowledge-based question to get to the bottom of something and truly understand each step. I not make (or intend to make) any assertions that this code is "good" or "bad". It simply is, and I wish to understand it. And I wanted to collect some down votes. – Jonathan Komar Nov 26 '17 at 14:40

1 Answers1

3

This happens because, in case of i++, the increment takes place as a side effect after the value computation for the expression i++ <10. Then, the loop body is executed and incremented value is printed inside the loop body.

Quoting C11, chapter §6.5.2.4,

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). [...] The value computation of the result is sequenced before the side effect of updating the stored value of the operand. [...]

and, for better understanding, quoting chapter §6.8.5.3, the for loop

The statement

for ( clause-1 ; expression-2 ; expression-3 ) statement

behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. [....]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • While your statement is obviously correct, I don't think it's going to help OP very much, given item #1 in the question. – Mad Physicist Nov 26 '17 at 14:20
  • @MadPhysicist How's that? – Sourav Ghosh Nov 26 '17 at 14:20
  • OP seems to know that the operator works after the statement, but seems to think that it defers to after the loop is done. Notice the omitted last section of the loop definition. – Mad Physicist Nov 26 '17 at 14:22
  • Thanks for trying to get to the bottom of this Mad Physicist. If find this answer helpful, more or less, because it confirms what I assumed from my observation. It references an authoritative source and directly answers my question about when the comparison takes place and when the value of `i` actually gets incremented (it would seem immediately as soon as the loop body begins as opposed to within the comparison). – Jonathan Komar Nov 26 '17 at 14:24
  • 2
    @Jonathan - The increment actually *does* happen as part of the comparison expression. It's just that `i++` increments and uses the **old** value, while `++i` increments and uses the **new** value. – Bo Persson Nov 26 '17 at 15:08