D
Darrell Grainger
Is this a bug in my compiler or am I mistaken?
If I have the following code:
#include <stdio.h>
void test(void)
{
static int i;
printf("%d ", i++);
if(i < 5) test();
}
int main(void)
{
test();
printf("\n");
return 0;
}
If I load and run it I get "0 1 2 3 4 \n" printing out. If I load and
running it a second time I get "5 \n" printing out. I know that &i is the
same on each run of the program. It seems to be retaining the last value
and not clearing the memory location. The fact that I got the correct
output the first time seems like luck.
I thought the standard guarantees that static variables will be
initialized to 0. Am I wrong? Shouldn't this print out the same every
time it is run?
If I have the following code:
#include <stdio.h>
void test(void)
{
static int i;
printf("%d ", i++);
if(i < 5) test();
}
int main(void)
{
test();
printf("\n");
return 0;
}
If I load and run it I get "0 1 2 3 4 \n" printing out. If I load and
running it a second time I get "5 \n" printing out. I know that &i is the
same on each run of the program. It seems to be retaining the last value
and not clearing the memory location. The fact that I got the correct
output the first time seems like luck.
I thought the standard guarantees that static variables will be
initialized to 0. Am I wrong? Shouldn't this print out the same every
time it is run?