-6

In this for loop statement

#include<stdio.h>

int main()
{
  static int i; 
  for(++i;++i;++i) { 
    printf("%d ",i); 

    if(i==4) 
       break; 
 } 
 return 0; 

}

Variable i is at first 0. The arguments in the for-loop at 1st round are
1st ++i: i = 0 + 1 = 1
2nd ++i: i=1+1=2
So, in first loop I have this for(i=1; i<2; ++i); or for(i=1; i<=2; ++i);?

EDIT I found this example online in a test about C. I run this (inside the for-loop , I have a break point so after some loops it breaks) but I was just guessing the behavior of that so I asked it here to be sure. I am learning now C so stupid questions exists for me. Its better to ask, than not.

yaylitzis
  • 5,354
  • 17
  • 62
  • 107

4 Answers4

3

In the second argument it is actually ++i!=0, The loop is interpreted as

for(++i;++i!=0;++i)

If you start with a positive i or 0, it will be an infinite loop and will invoke undefined behavior when i reaches INT_MAX.

If i was -Ve initially the loop may stop at a defined run.

EDIT: As you changed your question, Your code will not crash, but you can clearly understand the dry-run by replacing the second ++i with ++i!=0.

So the 1st iteration becomes:

(i=1;2!=0;++i/*this will execute later*/)

2nd iteration becomes:

i=3 //this is from the 1st iteration last part.
(/*initialization is done 1st time only*/;4!=0;++i/*again, this will execute after this iteration*/)
Dipto
  • 2,720
  • 2
  • 22
  • 42
2

It will print 2 4.

Before the for loop, i will be 0. It hasn't been assigned anything yet, and static variables are guaranteed to be zero initialized before they are first used.

It will execute the first ++i in the for loop, since that expression is evaluated once at the beginning of the loop. i will be 1.

It will execute the second ++i, because that is evaluated BEFORE every loop to see if it should run an iteration of the loop. i will be 2.

It will run the loop body. This will print 2.

The if condition won't be true so it won't break.

It will execute the third ++i in the for loop statement, since it evaluates that AFTER every iteration. i will be 3.

It will execute the second ++i again, since it needs to see if it needs to run another loop. It will be nonzero, so it will run another loop. i will be 4.

It will print 4.

The if condition will be true, it will break out of the loop.

However, it is a nonsense way to do it. This is a more appropriate way to do that:

int i;
for (i = 2; i <= 4; i += 2)
    printf("%d ", i);

or better yet:

printf("2 4 ");
doug65536
  • 6,562
  • 3
  • 43
  • 53
  • I know that this is very stupid way to do this. I just found the example in an online test about C and I was curious about that. – yaylitzis Jan 20 '14 at 10:12
  • 1
    Hopefully my walk-through helps you understand what those three things are in the `for` statement. – doug65536 Jan 20 '14 at 10:15
1
   static int i; 

While the C standard guarantees that variables with static storage duration are initialized to 0, you should not abuse that. Always initialize your variables, either at the line where they are declared or in runtime. So change this to static int i=0;

The first ++i is indeed equivalent to having i=1 there. Esentially your loop does this:

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

If you have a break inside the loop, then the loop is likely poorly written. If you know in advance when the loop should end, then that condition should be inside the for loop condition. If you don't know in advance, then use a while loop instead.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

It should be :

for(i=1; i<=2; ++i);
static int i=0;
for(++i;++i<=2;++i)
{
      printf("4rth :%d\n",i);
}

see : http://ideone.com/TGLYlL

shifu
  • 6,586
  • 5
  • 21
  • 21