gpt4 book ai didi

c - 尝试理解 printf 带有字符说明符 (%c) 的指针

转载 作者:行者123 更新时间:2023-11-30 18:17:04 25 4
gpt4 key购买 nike

#include<stdio.h>
void main()
{
int n = 2;
printf("%c",&n);
}

输出:L

在使用%d时,它当然给出了变量n的地址,但为什么在使用%c时它输出L?

最佳答案

这是未定义的行为,因为您使用了错误的格式说明符

C11 标准:7.21.6.1:第 9 段:

If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.

这意味着如果使用错误的格式说明符调用printf,任何事情都可能发生。

关于c - 尝试理解 printf 带有字符说明符 (%c) 的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45165718/

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