I have code similar to the following in our product. According to me, the output is '0 1 2 3'. But the output of the similar code is '1 1 1 1'.
for(i = 0 ;i < 5;i++){
int j;
if(i)
printf("%d ",j);
j = i;
}
My understanding is that the j is allocated on the stack only once during the entire period of 'for' loop and the same value is used during iterations. Also, if I move the declaration of j outside for loop, I'm getting the expected result. What am I missing here?
PS - When I run the same code on my personal machine, I am getting the expected output. But on production it is different.
First, to clear things about the storage duration of an automatic local variable, let me quote the
C11
standard, chapter §6.2.4, (emphasis mine)and,
So, in your code, each iteration gets a new instance of
j
. Nothing is retained.In your code,
you're trying to use an unitialized automatic local variable
j
, which has indeterminate value. It invokes undefined behavior.As per
C11
, chapter §6.7.9and related, for UB, annex §J.2
Once your code hits UB, the output cannot be justified, anyway.
OTOH, when you declare
j
outside the loop, it has function scope. Then, unlike above case, there will be only one instance ofj
for all iterations of the loop.As per the execution flow, first time,
i
being 0,if
will evaluate to false,printf()
will be skipped andj
will get initialized. Then, in next iteration, when you hit theprintf()
,j
is initialized and it's all well thereafter.