2

Code:

int main()  
{
  int a=1;
  switch(a)
  {
    int b=20;

    case 1:
    printf("b is %d\n",b);
    break;

    default:
    printf("b is %d\n",b);
    break;
  }
  return 0;
}

Output: It prints some garbage value for b when does the declaration of b takes place here Why b is not initialized with 20 here???

Lundin
  • 195,001
  • 40
  • 254
  • 396
Luv
  • 5,381
  • 9
  • 48
  • 61
  • @0A0D It's not a duplicate of that question. The initialization is in a different part of the switch (inside a case), and the top voted answer doesn't apply here. – Paul Mar 16 '12 at 13:38
  • @0A0D: No it is not a duplicate of that question. The answer to that is it's a syntax error to follow a case label with a variable declaration. Whereas this because the switch causes the initialiser code to be skipped. – JeremyP Mar 16 '12 at 13:39
  • @PaulP.R.O.: See this answer in the question: http://stackoverflow.com/a/8550253/195488 –  Mar 16 '12 at 13:54
  • @0A0D Still isn't nearly as good an answer in context as refp's answer below. I believe this question should remain open. – Paul Mar 16 '12 at 14:20
  • @PaulP.R.O.: Quality is another issue. –  Mar 16 '12 at 14:27
  • @0A0D The fact that the two questions are different is my issue. I never said refp's answer was higher quality. I said it's higher quality in the context of this question, which is a different question than the one you closed this as a duplicate of. – Paul Mar 16 '12 at 16:32
  • @PaulP.R.O.: I didn't close, I am one of 5 voting members. Apparently others thought the same too. "Still .. nearly.. good an answer" infers quality. –  Mar 16 '12 at 16:45
  • @0A0D And context infers context... You were the first to close and the one who picked the other "duplicate". This discussion is getting pretty META anyways. – Paul Mar 16 '12 at 17:00
  • @PaulP.R.O.: Ok, blame it on me. You can always vote to re-open and then this all is moot. –  Mar 16 '12 at 17:54

7 Answers7

8

Because memory will be allocated for int b but when the application is run "b = 20" will never be evaluated.

This is because your switch-statement will jump down to either case 1:1 or default:, skipping the statement in question - thus b will be uninitialized and undefined behavior is invoked.


The following two questions (with their accepted answers) will be of even further aid in your quest searching for answers:


Turning your compiler warnings/errors to a higher level will hopefully provide you with this information when trying to compile your source.

Below is what gcc says about the matter;

foo.cpp:6:10: error: jump to case label [-fpermissive]
foo.cpp:5:9: error:   crosses initialization of 'int b'

1 since int a will always be 1 (one) it will always jump here.

2 most relevant out of the two links, answered by me.

Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
3

Switch statements only evaluate portions of the code inside them, and you can't put code at the top and expect it to get evaluated by every case component. You need to put the b initialization higher in the program above the switch statement. If you really need to do it locally, do it in a separate set of braces:

Code:

int main()  
{
  int a=1;
  /* other stuff */
  {
    int b=20;
    switch(a)
    {

      case 1:
      printf("b is %d\n",b);
      break;

      default:
      printf("b is %d\n",b);
      break;
    }
  }
  /* other stuff... */
  return 0;
}
Wes Hardaker
  • 21,735
  • 2
  • 38
  • 69
2

The switch directly jumps to case 1:, never executing the assignment.

Femaref
  • 60,705
  • 7
  • 138
  • 176
2

Presumably because switch functions like a goto - if a == 1, it jumps straight to case 1: and bypasses initialization of b.

That is: I know switch jumps straight to the case label, but I'm very surprised the compiler doesn't complain about the missed initialization.

Useless
  • 64,155
  • 6
  • 88
  • 132
1

It's a pretty bad idea to initialize B under the switch statement and outside a case statement. To understand what's going on here, you have to know that the switch makes a jump to the correct case/default statement.

ALOToverflow
  • 2,679
  • 5
  • 36
  • 70
1

because when switch(a) statement executes control goes directly to the statement case 1: without executing statement int b=20, taht's why it gives garbage value as answer. If u want to print a then either u have to initialise in case 1: block or u have to initialise to before switch(a) statement.

Rajesh
  • 764
  • 8
  • 16
0

Because that line is never reached. When C hits a switch(a) statement, it branches to the case that matches the condition of the variable you're switching on. The statement that initialises b isn't in any of the cases. I suppose the compiler is free to write 20 into the location, but the language doesn't require it to do so and in this case it doesn't: it's also free to leave initialisation until it actually executes an assignment.