user3335653
user3335653

Reputation: 133

for loop first statement should be declaration

main ()
{
      char i = 0;
      for (i <= 5 && i >= -1; ++i; i > 0)
          printf ("%d", i);
      getch ();
}

I am a Java student and recently I started doing C programs. I saw this question in a C book. I have the following doubts about this program:

  1. This program compiles well. Should this not cause a compiler error? The first part of the for loop should be a declaration but here it is a boolean expression.
  2. The output is 1,2,3,4.....126,127,-128....-2,-1. Why does the output stop at -1? This should be an infinite series, repeating the above series forever.

Upvotes: 0

Views: 167

Answers (3)

Kninnug
Kninnug

Reputation: 8053

(Some people are going to have to eat some weird stuff now)

When compiling with GCC-4.8.1 on MinGW32 (flags: -Wall -pedantic -std=c99) I get only the following warnings:

In function 'main':
6:7: warning: statement with no effect [-Wunused-value]
       for (i <= 5 && i >= -1; ++i; i > 0)
       ^
6:7: warning: statement with no effect [-Wunused-value]

This can be explained as follows. As mentioned in the comments, the syntax for a for-loop is just for(expression-1; expression-2; expression-3) (where each of the expressions can also be empty). Moreover the for-loop can be viewed as just syntactic-sugar for a while-loop and it is indeed translatable to:

expression-1;
while(expression-2){
    body;
    expression-3;
}

In your case it translates to:

i <= 5 && i >= -1;
while (++i) {
    printf ("%d", i);
    i > 0;
}

GCC then warns about expression-1 and expression-3 since they valid and produce a value, but the value is discarded and the expressions have no side-effects. When optimizing the compiler will probably not even include these expressions in the generated code since they have no effect.

The reason it doesn't loop forever is because eventually ++i will overflow and the compiler/platform/the gnomes in the machine have decided to make it wrap around so that it becomes negative and eventually goes back up to 0: stopping the loop. Note that this is strictly speaking undefined-behaviour since it invokes signed-integer-overflow.

Upvotes: 4

Marian
Marian

Reputation: 7482

For loop is composed of three expressions, so this example compiles well. The first one is i <= 5 && i >= -1. It is doing nothing. The second one is the condition when the cycle shall stop. In your case ++i means that the cycle stops when i reaches the value 0. The third part i>0 does nothing. So your cycle:

  for (i <= 5 && i >= -1; ++i; i > 0)
      printf ("%d", i);

is equivalent to:

  i <= 5 && i >= -1;
  while (++i) {
      printf ("%d", i);
      i>0;
  }

which is equivalent to:

  while (++i) {
      printf ("%d", i);
  }

Upvotes: 6

Klas Lindb&#228;ck
Klas Lindb&#228;ck

Reputation: 33283

  1. The first part can be any valid C expression. It can even be left blank. It is custom to use it to assign an initial value to the loop variable, though.

  2. A char overflows pretty quickly, and when the value of the second expression, ++i reaches 0 the loop stops.

Upvotes: 3

Related Questions