3

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?

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 2
    **[My answer](http://stackoverflow.com/a/8535765/1090079)** to [How can a variable be used when its definition is bypassed?](http://stackoverflow.com/q/8535731/1090079) should explain what is happening – Filip Roséen - refp Dec 19 '11 at 05:04
  • 1
    Note that if the declaration of `b` was `int b = 31`, there'd be no guarantee that the initialization would occur. The main difference between the `printf()` and the declaration is that the `printf()` statement is executable and the declaration is not. – Jonathan Leffler Dec 19 '11 at 05:32

2 Answers2

8

Think of switch as just a goto with labels. It doesn't matter where you goto, as long as a variable declaration is above where you use it, you can use it. This is partially due to the fact that a variable declaration is not an executable statement that gets "done" like an expression. That switch is very nearly equivalent to:

int a  = 1;

{
    if (a == 1) goto case1;
    goto defaultcase;

    int b;

case1:
    b = 34;
    printf("%d", b);
    goto end;

defaultcase:
    goto end;

end:
}

return 0;

And the gotos have nothing to do with the scope of b.

Try doing this though:

switch (a)
{
int b = 1;
....

In that case, even though b is declared, the initialisation will be skipped because that is an executable statement that can either be done or not done. Your compiler should warn you if you try to do that.

Regarding the declaration inside if (updated question): In that case, a has a scope limited to the if. It is created when the scope is entered, and is destroyed when the scope ends.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 2
    @Mr.32 in that case, `a` as a scope limited to the `if`. It is created when the scope is entered, and is destroyed when the scope ends. Note that `switch`es do not create a new scope, exactly like labels and `goto`. – Seth Carnegie Dec 19 '11 at 09:03
  • 1
    @SethCarnegie: Shouldn't this (scope/if) be added to the answer in order to reflect the changes of the question? – undur_gongor Dec 19 '11 at 09:26
  • @undur If you think it should then I will add it tomorrow, or you can if you have time and energy. Anyone is welcome and should feel free to edit my answers. – Seth Carnegie Dec 19 '11 at 09:33
  • @SethCarnegie: Copying the "switches do not create new scope" part I think it is wrong. 6.8.4: "A selection statement is a block whose scope is a strict subset of the scope of its enclosing block." applies to both `if` and `switch`. – undur_gongor Dec 19 '11 at 10:19
  • @undur_gongor yes I'm sorry, the example was flawed and is now correct, I meant to indicate that each `case` didn't have a new scope. – Seth Carnegie Dec 19 '11 at 15:05
2

Because int b; is a declaration and the compiler will not generate code for it.

int a = 1;

switch (a) {

/* Instructions corresponding to the code here will not be executed */
/* printf("hi"); Instructions for such code will never be executed */
/* int b;      It is just a declaration. No executable code is generated for it*/

int b = 34;/*Replaced int b by int b = 34 now b will never be initialized to 34*/
case 1:
   printf("%d",b);      /*you will get garbage value now*/
   break;
default:
   break;
}
undur_gongor
  • 15,657
  • 5
  • 63
  • 75
Ameliorator
  • 417
  • 1
  • 5
  • 10