C++ bool 的 printf 格式说明符是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17307275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:04:55  来源:igfitidea点击:

What is the printf format specifier for bool?

c++cbooleanprintf

提问by maxschlepzig

Since ANSI C99 there is _Boolor boolvia stdbool.h. But is there also a printfformat specifier for bool?

由于 ANSI C99 有_Boolbool通过stdbool.h. 但是还有printfbool的格式说明符吗?

I mean something like in that pseudo code:

我的意思是类似于那个伪代码:

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

which would print:

这将打印:

true

回答by

There is no format specifier for booltypes. However, since any integral type shorter than intis promoted to intwhen passed down to printf()'s variadic arguments, you can use %d:

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

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

But why not:

但为什么不:

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

or, better:

或更好:

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

or, even better:

或者,甚至更好:

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

instead?

反而?

回答by Ivaylo Strandjev

There is no format specifier for bool. You can print it using some of the existing specifiers for printing integral types or do something more fancy:

bool 没有格式说明符。您可以使用一些现有的说明符来打印它来打印整数类型或做一些更花哨的事情:

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

回答by maxschlepzig

ANSI C99/C11 don't include an extra printf conversion specifier for bool.

ANSI C99/C11 不包括额外的 printf 转换说明符用于bool.

But the GNU C library provides an API for adding custom specifiers.

但是GNU C 库提供了用于添加自定义说明符的 API

An example:

一个例子:

#include <stdio.h>
#include <printf.h>
#include <stdbool.h>

static int bool_arginfo(const struct printf_info *info, size_t n,
    int *argtypes, int *size)
{
  if (n) {
    argtypes[0] = PA_INT;
    *size = sizeof(bool);
  }
  return 1;
}
static int bool_printf(FILE *stream, const struct printf_info *info,
    const void *const *args)
{
  bool b =  *(const bool*)(args[0]);
  int r = fputs(b ? "true" : "false", stream);
  return r == EOF ? -1 : (b ? 4 : 5);
}
static int setup_bool_specifier()
{
  int r = register_printf_specifier('B', bool_printf, bool_arginfo);
  return r;
}
int main(int argc, char **argv)
{
  int r = setup_bool_specifier();
  if (r) return 1;
  bool b = argc > 1;
  r = printf("The result is: %B\n", b);
  printf("(written %d characters)\n", r);
  return 0;
}

Since it is a glibc extensions the GCC warns about that custom specifier:

由于它是 glibc 扩展,因此 GCC 会警告该自定义说明符:

$ gcc -Wall -g    main.c   -o main
main.c: In function ‘main':
main.c:34:3: warning: unknown conversion type character ‘B' in format [-Wformat=]
   r = printf("The result is: %B\n", b);
   ^
main.c:34:3: warning: too many arguments for format [-Wformat-extra-args]

Output:

输出:

$ ./main
The result is: false
(written 21 characters)
$ ./main 1
The result is: true
(written 20 characters)

回答by jxh

In the tradition of itoa():

在传统中itoa()

#define btoa(x) ((x)?"true":"false")

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

回答by Stephan

You can't, but you can print 0 or 1

你不能,但你可以打印 0 或 1

_Bool b = 1;
printf("%d\n", b);

source

来源

回答by Arsen Y.M.

If you like C++ better than C, you can try this:

如果你比 C 更喜欢 C++,你可以试试这个:

#include <ios>
#include <iostream>

bool b = IsSomethingTrue();
std::cout << std::boolalpha << b;

回答by Tarion

To just print 1 or 0 based on the boolean value I just used:

根据我刚刚使用的布尔值仅打印 1 或 0:

printf("%d\n", !!(42));

Especially useful with Flags:

对标志特别有用:

#define MY_FLAG (1 << 4)
int flags = MY_FLAG;
printf("%d\n", !!(flags & MY_FLAG));

回答by xjzhou

I prefer an answer from Best way to print the result of a bool as 'false' or 'true' in c?, just like

我更喜欢Best way to print the result as 'false' or 'true' in c? , 就像

printf("%s\n", "false##代码##true"+6*x);
  • x == 0, "false\0true"+ 0" it means "false";
  • x == 1, "false\0true"+ 6" it means "true";
  • x == 0, "false\0true"+ 0" 表示“假”;
  • x == 1, "false\0true"+ 6" 表示“真”;