c - C 输出背后的逻辑

标签 c

为什么以下程序的输出是

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/

相关文章:

c - 如何在 C 中的标准输入上使用 fgets() 而不是 fscanf()?

c++ - 将事件设置为零的文件描述符添加到 epoll 是否有效?

c - 从数据到 OpenGL 二维纹理

c - bsearch() 按名称比较

c++ - 如何静音/取消静音 BASS?

c - 将两个数字 [1, 10^10000] 作为字符数组相加 - C

C windows _popen on 命令请求输入 : avoid freeze

C 只读取第一个结构体,无法读取其他输入

c - 如何在我的 Make 文件中定义#define

MySQL C API : Getting `MYSQL_FIELD` array from `MYSQL_STMT` ?