top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Local variable inside a loop in C?

+2 votes
952 views

I have a doubt regarding variable declaration inside a for loop.

My understanding is, the statements inside loop will be executed repeatedly till it exit from the loop.

My doubt:
if we declare a variable inside a for loop, will it be created every time the loop executes? or during the compilation process the local variable declaration and initialization will be moved to different section.

Example

    for(i=0;i<10;i++) {
            int j;
            ...
    }
posted Nov 18, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Dear your understanding is right. anything inside the loop get executed no of time loop runs. So it is true every time you are allocating variable(variable creation ). and goes out of scope so removing.

answer Nov 18, 2014 by Naren Badsara
Hi Naren.
As per my understanding, after executing the body of the for loop, the control variable (in this case "i") is incremented/decremented. The control does not go out of the loop until the condition fails. And as we know that the life of an auto variable lies within a block. I know the compiler does not generate any error (redefinition). My doubt is that when the control does not go out of the for loop then how does the variable gets destroyed & why doesn't the compiler generate the error "redefinition of j"?
+1 vote

Hi,
If we declare a variable inside a for loop, will it be created every time the loop executes??
Yes it will be created in the stack memory everytime when the loop executes.

When ever for loop completes one iteration that means that block is completed and it is going to execute one the same block if for loop condition true. So When ever it encounters '}' brace block is completed and varilable "j" will be destroyed from memory.

answer Nov 19, 2014 by Naveen Yanamaddi
Thanks for the response Naveen !
Similar Questions
+1 vote

What is the overhead in splitting a for-loop like this,

int i;
for (i = 0; i < exchanges; i++)
{
    // some code
    // some more code
    // even more code
}

into multiple for-loops like this?

int i;
for (i = 0; i < exchanges; i++)
{
    // some code
}
for (i = 0; i < exchanges; i++)
{
    // some more code
}
for (i = 0; i < exchanges; i++)
{
    // even more code
}

The code is performance-sensitive, but doing the latter would improve readability significantly. (In case it matters, there are no other loops, variable declarations, or function calls, save for a few accessors, within each loop.)

I'm not exactly a low-level programming guru, so it'd be even better if someone could measure up the performance hit in comparison to basic operations, e.g. "Each additional for-loop would cost the equivalent of two int allocations." But, I understand (and wouldn't be surprised) if it's not that simple.

+5 votes

Even I have similar problem:

int (*pfun)(void);
int *pInt = 0;
void fun1()
{
    int i = 5; /* Local to fun1*/

    printf("Outer function");
    pInt = &i; /* As I know address of local variable is valid till function execution */

    int fun2()
    {
      printf("innerfunction");
      printf("%d", *pInt);
    }
    /* fun2 address assign to pfun so that It can be called even after completion of fun1 */
    pfun = fun2;
}

int main()
{
    fun1();
    pfun();
    return 0;
}

Can someone please explain ? I am getting *pInt value 5.

...