3

In c, when this block of code is run,it outputs 10 10 10 10 10. I think the loop should exit after 1st execution as i becomes 11 but it is not happening. Why it is so?

#include<stdio.h>
int main()
{
  int i; 
  for(i=0;i<5;i++)
  {
     int i=10;  
     printf("%d\t",i);
     i++;
  }
  return;
}

But when program is written as below the output is similar to what i am thinking(i.e.10 ).What is the exact difference between above code and the code shown below? How C is handling these variable? I would be glad if anyone explain about this.

#include<stdio.h>
int main()
{
  int i; 
  for(i=0;i<5;i++)
  {
     i=10;  
     printf("%d\t",i);
     i++;
  }
  return;
}
2
  • 3
    You shouldn't do return; from main() — it should have a value returned, and the compiler should be complaining. Commented Dec 28, 2013 at 14:58
  • That should be return 0. Commented Dec 28, 2013 at 15:00

4 Answers 4

5

In your first program, for loop is using the i declared outside the for to control the loop. The printf uses the i declared inside the for loop to print the value and this i has block scope.

The new declaration of i in the for loop temporarily hides the old declaration. Now the value of i is 10. At the end of the for loop block the new i is not visible to the program and the variable regains its old meaning and this time i stores the value as per the iteration of loop (either 1, 2, 3 or 4).

Sign up to request clarification or add additional context in comments.

4 Comments

...as for(), while() and most control structures, except switch ... case (which uses labels and not blocks) in C are separate scopes with the condition/control block being a extra scope between the outer and inner scope. Thus variables defined in the control block are valid in the control block and the structure body.
@dom0: To be pedantic, the "inner scope" is not a special part of the control structure. A control structure takes a statement, and in this case it happens to be a block statement. A block statement creates a new scope. It's no different to e.g. int main(void) { int i; { int i; ... } }.
So,new declaration inside the for loop hides the previous declaration of i and when control reaches to the update section of for loop, i declared inside the loop is hidden.? @haccks
@Kindle; Yes. Exactly.
3

In the first code you declare your variable twice: one outside the loop and the second one inside the loop. So inside the loop compiler found another declaration for the variable so it uses the inner declaration (this called block scope).

So that the first program print 10 10 10 10 10 because this is the inner declaration int i=10

But in the second code you declare it once so the compiler use this declaration in the whole program.

Comments

2

In C99 or later, you can write this variation on your code with 3 independent variables called i:

#include <stdio.h>
int main(void)
{
    int i = 19;
    printf("i0 = %d\n", i);
    for (int i = 0; i < 5; i++)
    {
        printf("i1 = %d\n", i);
        int i = 10;
        printf("i2 = %d\n", i);
        i++;
    }
    printf("i0 = %d\n", i);
    return 0;
}

The output is:

i0 = 19
i1 = 0
i2 = 10
i1 = 1
i2 = 10
i1 = 2
i2 = 10
i1 = 3
i2 = 10
i1 = 4
i2 = 10
i0 = 19

(In C89, you can't have the variable tagged i1 in the output, and you couldn't demonstrate the two variables in the loop because declarations have to precede statements such as printf() calls.)

Comments

1

Because in first example the i inside the loop is not the same as the one which governs/controls the loop, it is a completely different variable though with same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.