简体   繁体   English

这个 C 程序是如何工作的?

[英]How this C program is work?

void main()
{
long x;
float t;
scanf("%f",&t);
printf("%d\n",t);
x=90;
printf("%f\n",x);
{
    x=1;
    printf("%f\n",x);
    {
        x=30;
        printf("%f\n",x);
    }
    printf("%f\n",x);
}
    x==9;
    printf("%f\n",x);
}

For I/P 7 it print 0 and six times 7. How this code is working?对于 I/P 7,它打印 0 和 6 次 7。这段代码是如何工作的? i know that behavior is undefined if we print variable with other than corresponding format specifier , but why this print 6 everytime ?我知道如果我们使用相应的格式说明符以外的其他格式打印变量,则行为是未定义的,但是为什么每次都打印 6?

"Undefined behavior" is just that -- undefined. “未定义的行为”就是——未定义。 The fact that it's undefined makes "why is it doing that?"它未定义的事实使“为什么要这样做?” an invalid question.一个无效的问题。

Without proper format specifiers its obvious that the output will be erratic.如果没有正确的格式说明符,很明显输出会不稳定。 As to "why" this happens rests on the compiler to decide.至于“为什么”发生这种情况取决于编译器来决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM