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