6

Simple Question. Imagine this in ANSI-C:

int i;

for(i=0 ; i<5 ; i++){
   //Something...
}

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

Will this output "i is 5" ?

Is i preserved or is the value of i undefined after the loop?

5 Answers 5

16

Yes. If i is declared outside of the for loop it remains in scope after the loop exits. It retains whatever value it had at the point the loop exited.

If you declatred I in the loop:

for (int i = 0 ; i < 5 ; i++)
{

}

Then i is undefined after the loop exit.

2

Variable i is defined outside of the scope of the loop (which is great, or you wouldn't be able to print it in that case).

And it is post-icnremented for every-turn of the loop, for which the end condition is "stop when i is bigger or equal to 5".

So it really makes perfect sense for i to be equal to 5 at this point.

A block scope is not exactly the same as a function scope in C. The variable i doesn't "get back" magically to its previous value when you step out of the loop's scope.

1

i's value will be 5 after your loop. Unless you did something like

i = 50000;

inside of it.

0

It's also generally recommended against using "i" after you exit the loop in most coding standards I have ever read. In particular do NOT do:

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        break;
    }
}

if(i == num_elements)
{
    fprintf(stderr, "Failed to find element %d.", id);
    succeeded == false;
}

While this will work it is poor coding. It is less readable and maintainable than the alternatives. E.g.

succeeded = false;

for(i = 0; i < num_elements; i++)
{
    if(element[i].id == id)
    {
        /* Do something to element here. */
        succeeded = true;
        break;
    }
}

if(false == succeeded)
{
    fprintf(stderr, "Failed to find element %d.", id);
}
0

Yes, variables are valid only inside the block in which they are declared. Here's an example:

#include <stdio.h>

void main(int argc, char *argv[])
{
    if(argc == 2) {
        int x;
        x = 7;
    }

    x = 1;
}

That's the compiler:

gcc ex.c
ex.c: In function ‘main’:
ex.c:10: error: ‘x’ undeclared (first use in this function)
ex.c:10: error: (Each undeclared identifier is reported only once
ex.c:10: error: for each function it appears in.)
2
  • Err, what? He asked "Is A the case or is B?". Such a question cannot satisfactorily be answered simply "Yes" or "No". Also he declared his variable outside the block. I don't find this answer relevant or necessary (as it adds no new information not already contained in previous answers).
    – AlastairG
    Commented Dec 1, 2010 at 15:18
  • Yeah, you're right. I simply answer 'yes' because someone already explained why i is preserved (Visage's answer was the best). I thought adding this example could be useful to complete other answers. =)
    – BlackBear
    Commented Dec 1, 2010 at 17:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.