【发布时间】:2020-05-01 10:58:01
【问题描述】:
有人能解释一下为什么代码打印的是 "HelloWorld" 而不是 "HelloWorldThere" 吗?另外为什么它打印任何东西,因为 if 或 switch 语句中没有条件?代码如下:
#include <stdio.h>
int main()
{
int a, b;
if(printf("Hello"))
switch(printf("World"))
while(printf("There"))
{
return 0;
}
}
【问题讨论】:
-
您可能想了解
printfreturns 的内容。 -
还有关于
switch的声明……你的代码完全是一派胡言。 -
关于
switch:“如果表达式的计算结果与以下任何一种情况都不匹配:标签,并且默认值:标签不存在,则不会执行任何开关体。” -- 没有case标签,所以没有匹配表达式,也没有default标签,所以没有任何反应。 -
我已经专业地编写 C 语言近 20 年了,但从未意识到这一点:stackoverflow.com/questions/29023601/…。 (编辑以提供更好的链接)
-
@EdHeal: Yes, it compiles and runs,并产生 OP 描述的输出。
标签: c if-statement while-loop switch-statement printf