0

The semicolon at the end of the for loop is suppose to empty the body and create a null loop. But why this is printing 6?

void main()
{
    int i;
    for(i=1;i<=5;i++);
    {
        printf("%d\n",i);
    }
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Maths Maniac
  • 83
  • 1
  • 7
  • 2
    It makes null loop(do nothing), but it does increment `i` to 6, and so after loop ends next print statement is executed – Priyesh Kumar May 29 '17 at 18:27
  • 1
    You probably wanted to ask why there was only one number printed instead of 5, or you really don't get loops. – zubergu May 29 '17 at 18:28
  • 1
    It would be more accurate to say that the empty statement terminated by the semicolon *is* the loop body. This is not some kind of special case or magic significance of the semicolon. It simply creates a `for` loop whose *body* does nothing, but that works in all respects like any other `for` loop. – John Bollinger May 29 '17 at 18:29
  • Yoiu have a loop. What do you expect? – too honest for this site May 29 '17 at 18:32
  • @zubergu no i know loops,i am asking why it is printing 6 now i got it. – Maths Maniac May 29 '17 at 18:35
  • @MathsManiac what else do you expect to be printed? You have a loop that does nothing except incrementing `i`. After the loop you print `i`. What do you expect to happen **after** the loop? – Gerhardh May 30 '17 at 07:41

6 Answers6

11

The loop body is empty, otherwise it would print 1, 2, 3, 4, 5. But the loop head runs nethertheless and in each iteration it increases i. When it reaches 6 which is not <=5 the loop ends. Printing i after the loop prints i as 6. Incrementing i is a side effect of the loop.

Kriegel
  • 423
  • 5
  • 16
1

It is.at the end of the loop i will be 6 and the printf does this.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

The for loop for(i=1;i<=5;i++); will run exactly 5 times, incrementing i from 1 to 6 (even though the for loop body is a no-op). Thus, in here:

{
    printf("%d\n",i);
}

the program will print the current value of i, that is 6.

syntagma
  • 23,346
  • 16
  • 78
  • 134
1

Try this for fun

#include <stdio.h>

int main(void)
{
    int i;
    for (i = 1; i <= 5; i++) /* void */;

    /* floating block one */
    {
        int i = 42; /* new i, hides old i */
        printf("%d\n",i);
    }

    /* floating block two */
    {
        printf("%d\n",i);
    }
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • Explain me this @pmg – Maths Maniac May 29 '17 at 18:43
  • @Magnus see this void main() { int i; for(i=1;i<=5;i++); { int i = 42; printf("%d\n",i); } printf("%d\n",i); } – Maths Maniac May 29 '17 at 18:49
  • It should print 42 42,but printing 42 6 – Maths Maniac May 29 '17 at 18:50
  • 1
    The second definition of `i` (the `42`), inside the "floating" block, creates a different variable efectively hiding the first one (there are two `i`s, but you can only access the innermost). When that block finishes, the other "floating" block starts with the previous `i` in scope. – pmg May 29 '17 at 18:52
  • How can it get back the previous valur that is "2"?It is supposed to be replaced with 42. – Maths Maniac May 29 '17 at 18:55
  • how 42 is being replaced by 6. @pmg – Maths Maniac May 29 '17 at 19:07
  • The `6` is not replaced by anything. It "lives" at the same time as the `42`, both have the same name but different scopes (and lifetimes). The `i` with value 42 exists and is available only inside its floating block. The `i` with value 6 exists inside all of the `main()` function, but is hidden inside the floating block of the other `i`. – pmg May 29 '17 at 19:13
1

Because you declare the int outside the null loop, the value is saved outside the increment loop.

Read more about it here

The extra brackets do not do anything here because the semicolon exits the loop.

Read more about brackets here.

pastaleg
  • 1,782
  • 2
  • 17
  • 23
1

it is quite simple:

for(i=1;i<=5;i++); will be executed 5 times, from 1 to 5 then i=6 ends the for loop and then a new "scoped" statement is executed:

printf("%d\n",i);

therefore prints 6

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97