C 代码块输出的结果不正确

C 代码块输出的结果不正确,c,C,我的代码: #include <stdio.h> int main(){ int a = 5; int b,c; if (a==4){ b = 6; c = 7; } printf("%d %d\n",b,c); } #包括 int main(){ INTA=5; int b,c; 如果(a==4){ b=6; c=7; } printf(“%d%d\n”,b,c); } 我知道结果应该是0,但代码块给出了答

我的代码:

#include <stdio.h>
int main(){
    int a = 5;
    int b,c;
    if (a==4){
        b = 6;
        c = 7;
    }
    printf("%d %d\n",b,c);
}
#包括
int main(){
INTA=5;
int b,c;
如果(a==4){
b=6;
c=7;
}
printf(“%d%d\n”,b,c);
}

我知道结果应该是0,但代码块给出了答案50 8。我尝试了联机编译器,得到了答案0。那么代码块有什么问题呢?我正在使用最新版本的代码块和gcc编译器。如何避免将来出现此类问题?

此程序中未定义b和c的值,因为它们在打印前未初始化

此程序中未定义b和c的值,因为它们在打印前未初始化

我知道结果应该是0

你猜错了

变量
b、c
自动的(存储说明符),未初始化的变量->不确定值->不保证其值->UB

在您的情况下,您得到了
50,8
您还可以得到其他值/打印垃圾/崩溃

我知道结果应该是0

你猜错了

变量
b、c
自动的(存储说明符),未初始化的变量->不确定值->不保证其值->UB


在您的例子中,您得到了
50,8
您还可以得到其他值/打印垃圾/崩溃…

变量
b
c
的值是垃圾,因为
if()
条件变为
false
。这意味着,这是未定义的行为

C11第6.7.9节初始化:

第10段:

如果未初始化具有自动存储持续时间的对象 显然,它的值是不确定的


b
c
变量的值是垃圾,因为
if()
条件变成了
false
。这意味着,这是未定义的行为

C11第6.7.9节初始化:

第10段:

如果未初始化具有自动存储持续时间的对象 显然,它的值是不确定的


好的,看看你的代码,一步一步地分析

    #include <stdio.h>
    int main(){
        int a = 5;//You've assigned the value of 5 to integer a.
        int b,c;//You've declared two integer variables "b" and "c".
        if (a==4)/*Compiler checks whether the if condition is true,which in your  case is not*/
        {
            b = 6;/*Assigning value 6 to the b variable but no effect as the if condition of your's is not satisfied*/

            c = 7;/*The same above condition applies here.*/
        }
        printf("%d %d\n",b,c);/*Here your trying to print the values of b and c, 

But remember you have not given them any value. So the compiler automatically prints the garbage value(random value). It need not be 0 all the time.*/  

return 0;//You forgot this step btw:)
    }
#包括
int main(){
int a=5;//您已将值5赋给整数a。
int b,c;//您已经声明了两个整型变量“b”和“c”。
if(a==4)/*编译器检查if条件是否为true,在您的例子中不是*/
{
b=6;/*将值6赋给b变量,但如果不满足条件,则不会产生任何影响*/
c=7;/*上述条件同样适用于此处*/
}
printf(“%d%d\n”,b,c);/*这里是您试图打印b和c的值,
但请记住,您没有给它们任何值。因此编译器会自动打印垃圾值(随机值)。它不必总是为0。*/
返回0;//您忘记了此步骤(顺便说一句:)
}

好的,看看你的代码,一步一步地分析

    #include <stdio.h>
    int main(){
        int a = 5;//You've assigned the value of 5 to integer a.
        int b,c;//You've declared two integer variables "b" and "c".
        if (a==4)/*Compiler checks whether the if condition is true,which in your  case is not*/
        {
            b = 6;/*Assigning value 6 to the b variable but no effect as the if condition of your's is not satisfied*/

            c = 7;/*The same above condition applies here.*/
        }
        printf("%d %d\n",b,c);/*Here your trying to print the values of b and c, 

But remember you have not given them any value. So the compiler automatically prints the garbage value(random value). It need not be 0 all the time.*/  

return 0;//You forgot this step btw:)
    }
#包括
int main(){
int a=5;//您已将值5赋给整数a。
int b,c;//您已经声明了两个整型变量“b”和“c”。
if(a==4)/*编译器检查if条件是否为true,在您的例子中不是*/
{
b=6;/*将值6赋给b变量,但如果不满足条件,则不会产生任何影响*/
c=7;/*上述条件同样适用于此处*/
}
printf(“%d%d\n”,b,c);/*这里是您试图打印b和c的值,
但请记住,您没有给它们任何值。因此编译器会自动打印垃圾值(随机值)。它不必总是为0。*/
返回0;//您忘记了此步骤(顺便说一句:)
}


“我知道结果应该是0”,读取未初始化变量的结果是。你为什么这么认为。。。。。具有自动存储功能的变量不归零。请将变量初始化为零。请不要过于匆忙地责怪工具。我如何避免将来出现此类问题?通过阅读手册,不做假设,编写更好的代码“我知道结果应该是0”,阅读未初始化变量的结果是。你为什么这么认为。。。。。具有自动存储功能的变量不归零。请将变量初始化为零。请不要过于匆忙地责怪工具。我如何避免将来出现此类问题?通过阅读手册,不做假设,编写更好的代码“b和c的值未定义”,问题就更严重了。整数的默认值是0,不是吗?@Magic105你从哪里得到的信息?:)打印前不会指定值,因此当这些变量printed@Magic105零代表全局变量,对于局部变量,它可以是任何值(垃圾值),因此在使用变量“b和c的值未定义”之前,需要显式地为变量分配一些值问题还不止于此。整数的默认值是0,不是吗?@Magic105你从哪里得到的信息?:)打印前不会指定值,因此当这些变量printed@Magic105零代表全局变量,对于局部变量,它可以是任何值(垃圾值),因此在使用变量之前,需要显式地为变量分配一些值。整数的默认值是0,不是吗?@Magic105 no,不适用于自动变量,仅适用于静态和全局变量。适用于静态和全局变量-是。对于局部(自动)-整数的NoDefault值是0,不是吗?@Magic105否,不适用于自动变量,仅适用于静态和全局变量..对于静态和全局变量-是。对于本地(自动)-现在“显式”是什么意思?@CIsForCookies您可以删除该词。实际上,您可以读取它,就像具有自动存储持续时间的对象没有初始化一样,其值是不确定的。