In the following code, why is the variable i
not assigned the value 1
?
#include <stdio.h>
int main(void)
{
int val = 0;
switch (val) {
int i = 1; //i is defined here
case 0:
printf("value: %d\n", i);
break;
default:
printf("value: %d\n", i);
break;
}
return 0;
}
When I compile, I get a warning about i
not being initialized despite int i = 1;
that clearly initializes it
$ gcc -Wall test.c
warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
printf("value %d\n", i);
^
If val = 0
, then the output is 0
.
If val = 1
or anything else, then the output is also 0.
Please explain to me why the variable i
is declared but not defined inside the switch. The object whose identifier is i
exists with automatic storage duration (within the block) but is never initialized. Why?