In principle, it should be treated as a new array every time. In practice, it is likely to carry the same values as it did in the previous iteration. If you wanted a completely new array every time the loop went round, you'd need to use malloc
or calloc
to allocate it1 (then free
at the bottom of the loop to avoid memory leaks).
Consider this test:
#include <stdio.h>
int main()
{
int i = 0, n = 10;
while(i++<n)
{
int a[10];
printf("before assignment: %d\n", a[2]);
a[2] = 5;
}
return 0;
}
On the first iteration, a[2]
could contain anything. In fact, reading the value before it is initialised is undefined behaviour. This is certainly something to be avoided, as it can lead to unpredictable consequences.
On the second iteration (and subsequent ones) it happened to contain 5 when I tested it (but there's no guarantee that it should).
If however, you initialised a
like this:
int a[10] = {0};
then all the elements of a
would be 0 each time.
1. Actually in practice, you may well end up with the same block of memory if you did this anyway. The behaviour is not to be relied upon either way.