gpt4 book ai didi

c - bool 的 printf 格式说明符是什么?

转载 作者:太空狗 更新时间:2023-10-29 16:13:33 26 4
gpt4 key购买 nike

自 ANSI C99 以来,_Boolbool 通过 stdbool.h。但是 bool 是否也有 printf 格式说明符?

我的意思是类似于伪代码中的内容:

bool x = true;
printf("%B\n", x);

这将打印:

true

最佳答案

bool 类型没有格式说明符。但是,由于任何小于 int 的整数类型在传递给 printf() 的可变参数时都会被提升为 int,因此您可以使用 %d:

bool x = true;
printf("%d\n", x); // prints 1

但为什么不呢:

printf(x ? "true" : "false");

或者,更好的是:

printf("%s", x ? "true" : "false");

或者,甚至更好:

fputs(x ? "true" : "false", stdout);

代替?

关于c - bool 的 printf 格式说明符是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17307275/

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