1

Let's say you wrote a for loop:

for (int i = 0; i < 10; i++)
   for (int j = 0; j < 10; j++)

Is that for loop creating 10 different j variables, and does it deallocate i and j after its done looping?

I have seen many people do this instead:

int i, j, k

for (i = 0; i < 10; i++)
   for (j = 0; j < 10; j++)
//..All The Loops..//

Is there any advantage of declaring the i j k variables before all of your loops, or is it just a personal preference?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Pixel Paras
  • 169
  • 5
  • I think you should take a look at this post https://stackoverflow.com/questions/7880658/what-is-the-scope-of-a-while-and-for-loop – NDM Jan 18 '22 at 06:23
  • The place of variable declaration in the C source has very little to do with when the compiler actually allocates that variable. In practice, it will reserve registers or stack space for those variables at some point before they are used, then re-use that same register/stack space over and over. – Lundin Jan 18 '22 at 07:42

2 Answers2

5

All of the variables in question are being created in automatic storage. They are destroyed when they go out of scope. The two examples are simply declaring the variables in different scopes.

In the first example, i is scoped to the outer loop, meaning i exists only while the loop is running. It is created when the loop begins, and it is destroyed when the loop ends:

for (int i = 0; i < 10; i++) { <- created here
    <statements>
} <- destroyed here

Same with j in the inner loop:

for (int i = 0; i < 10; i++) { <- i created here
    for (int j = 0; j < 10; j++) { <- j created here
       <statements>
    } <- j destroyed here
} <- i destroyed here

In the second example, the variables are scoped to the outer block which the loops exist in. So the variables already exist before the outer loop begins, and they continue to exist after the loop ends.

{
    ...
    int i, j, k; <- created here

    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
           ...

    ...
} <- destroyed here
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 1
    In C, scope and lifetime are orthogonal. I accept that the distinction can appear pedantic, especially for beginners, but it's real. – rici Jan 18 '22 at 06:30
  • So there isn't a advantage in declaring them outside of the loop in ``c`` or ``c++`` they get deleted the moment their curly brace ends. If i understood the answer correctly. – Pixel Paras Jan 18 '22 at 06:47
  • 1
    @Pixel Paras: if you need the last value of the iteration variable after the for loop has ended, then you must declare it before/outside the loop. – infinitezero Jan 18 '22 at 07:03
1

for (int i = 0; i < 10; i++) are only allowed in C99 or C11 mode, it's a more modern style.

You should declare i before use it when worked with very old compiler.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 3
    This is not an answer but rather a comment. You have 51 reputation, so you can comment now. – Jabberwocky Jan 18 '22 at 07:09
  • 1
    @Jabberwocky: I disagree, that answers the second question. Decalring before was not a still but required for old standards. (and so some people continue to use the old way). – Jarod42 Jan 18 '22 at 09:16