gpt4 book ai didi

c - C 输出背后的逻辑

转载 作者:行者123 更新时间:2023-11-30 20:02:10 24 4
gpt4 key购买 nike

为什么以下程序的输出是

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com