Let's see the problem by code:
code-1
#include <stdio.h>
int main(int argc, char *argv[])
{
int a =1;
switch (a)
{
printf("This will never print\n");
case 1:
printf(" 1");
break;
default:
break;
}
return 0;
}
Here the printf()
statement is never going to execute - see http://codepad.org/PA1quYX3. But
code-2
#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 1;
switch (a)
{
int b;
case 1:
b = 34;
printf("%d", b);
break;
default:
break;
}
return 0;
}
Here int b
is going to be declared - see http://codepad.org/4E9Zuz1e.
I am not getting why in code1 printf()
doesn't execute but in code2 int b
is going to execute.
Why?
Edit: i got that int b; is declaration and it is allocate memory at compile time so whether control flow reach there or not that declaration will be done.
Now see this code
#include<stdio.h>
int main()
{
if(1)
{
int a;
}
a =1;
return 0;
}
here int a is in control flow path still yet this not going to compile...why?