开发者

Variable declared and initialized in a switch statement

Why does this program not output 20?

#include<stdio.h>

int main() {
    int a = 1;
    switch (a) {
            int b = 20;
        case 1:
        {
            pr开发者_JAVA百科intf("b is %d\n", b);
            break;
        }
        default:
        {
            printf("b is %d\n", b);
            break;
        }
    }
    return 0;
}


Because the switch statement jumps to a relevant case, so the line int b = 20 will never be executed.


Your compiler should warn you about this. The initialization of 'b' is at the beginning of the switch statement, where it will never be executed -- execution will always flow directly from the switch statement header to the matching case label.


It doesn't output "b = 20" because b is set inside the switch statement and this instruction is never executed. What you want is this:

int b = 20;
switch (a) {
    case 1:
    {
        printf("b is %d\n", b);
        break;
    }
    default:
    {
        printf("b is %d\n", b);
        break;
    }
}


Gcc throws a warning saying that b is uninitialized when you call the printf()

you have to move "int b = 20" before the switch()


Inside of a switch is a hidden goto statement. So basically what is happening is really

int a=1;
if(a==1){ //case 1
  goto case1;
}else{ //default
  goto default;
}
int b=20;
case1:....


The code

int b = 20

is actually doing two things:

int b

and

b = 20

The compiler sets up a variable called b when it compiles the program. This is an automatic variable, which goes on the stack. But it does not assign it a value until the program execution reaches that point.

Before that point, it is unknown what value the variable has.

This would not be true for a global variable, or a static variable -- those are initialized when the program begins running.


Remember that case labels in switch statement are called "labels" for a reason: they are pretty much ordinary labels, just like the ones you can goto to. This is actually how switch works: it is just a structured version of goto that just jumps from switch to the appropriate label and continues execution from there. In your code you always jump over initialization of b. So, b never gets initialized.


Compiling with gcc (Using cygwin on Windows) gives a warning message -

warning: unreachable code at beginning of switch statement

and the output is undefined or garbage which clearly says that initialization portion of b is never executed and hence the undefined value.

Note: Question can be raised that if the code or line is unreachable, then why does not we get the error: 'b' undeclared.

The compiler checks for the syntax of the program (above checks if b is declared) and finds it correct (b is declared), although semantically/logically it is incorrect.

Probably in future the compilers may become even more smarter and will be able to detect these kind of errors


The line

int b=20;

Is not executed before the switch is entered. You would have to move it above the switch statement to get 20 output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜