为什么以下程序的输出是
0 and Garbage Value and Garbage Value
程序是:
#include<stdio.h>
int main()
{
int a=9,b=0,x,y;
x=a&b;
y=a||b;
printf("%d %d %d");
return 0;
}
为什么 %d 第一次打印 0 ?
最佳答案
您的代码printf("%d %d %d");
调用未定义的行为
来自 C99 标准的 7.19.6.1 fprintf 函数部分:
The fprintf function writes output to the stream pointed to by stream, under control of the string pointed to by format that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored. The fprintf function returns when the end of the format string is encountered.
此行为也适用于 printf()
函数(以及 sprintf()
和 snprintf()
函数)。
关于c - C 输出背后的逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343911/