Variable Definition Ignore in C [duplicate]

放肆的年华 提交于 2020-01-11 11:13:23

问题


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???


回答1:


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:

  • How can a variable be used when it's definition is bypassed? 2

  • Why can't variables be declared in a switch statement?


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.




回答2:


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;
}



回答3:


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




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/9738207/variable-definition-ignore-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!