Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
switch语句的问题_C_Switch Statement - Fatal编程技术网

switch语句的问题

switch语句的问题,c,switch-statement,C,Switch Statement,让我们通过代码来查看问题: 代码-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; } r

让我们通过代码来查看问题:

代码-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;
}
此处将声明
int b
——请参阅

我不明白为什么code1
printf()
中不执行,但在code2
intb
中将执行

为什么?

编辑: 我得到了int b;是声明,它是在编译时分配内存的,所以无论控制流是否到达那里,都将执行该声明。

现在看看这个代码

#include<stdio.h>

int main()
{
   if(1)
   {
    int a; 
   }

a =1;
return 0;
}
#包括
int main()
{
如果(1)
{
INTA;
}
a=1;
返回0;
}

这里int a仍然在控制流路径中,但它不会编译…为什么?

开关看作是带有标签的
转到。无论您转到哪里,只要变量声明位于您使用它的位置之上,您就可以使用它。这部分是因为变量声明不是像表达式一样“完成”的可执行语句。该开关几乎相当于:

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;
goto
s与
b
的范围无关

尽管如此,请尝试这样做:

switch (a)
{
int b = 1;
....
在这种情况下,即使声明了
b
,初始化也将被跳过,因为这是一个可以执行或不执行的可执行语句。如果您试图这样做,编译器应该警告您


关于
if
中的声明(更新问题):
在这种情况下,
a
的范围仅限于
if
。它在输入范围时创建,在范围结束时销毁。

因为
int b
是一个声明,编译器不会为其生成代码

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

要解释发生了什么,请注意,如果
b
的声明是
int b=31
,则不能保证会发生初始化。
printf()
和声明之间的主要区别在于
printf()
语句是可执行的,并且声明不是。@Mr.32在这种情况下,
a
作为范围限制在
if
中。它在输入范围时创建,在范围结束时销毁。请注意,
switch
es不会创建一个新的作用域,就像labels和
goto
@SethCarnegie:这个(作用域/if)不应该添加到答案中以反映问题的变化吗?@undur如果你认为应该,我明天会添加,或者如果你有时间和精力,可以添加。欢迎任何人来编辑我的答案。@SethCarnegie:复制“开关不创建新范围”部分我认为这是错误的。6.8.4:“选择语句是其范围是其封闭块范围的严格子集的块。”适用于
if
switch
@undur\u gongor是的,很抱歉,示例有缺陷,现在是正确的,我的意思是指出每个
案例
都没有新的范围。
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;
}