简体   繁体   English

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

[英]What is the printf format specifier for bool?

Since ANSI C99 there is _Bool or bool via stdbool.h .由于 ANSI C99 有_Boolbool通过stdbool.h But is there also a printf format specifier for bool?但是还有一个用于 bool 的printf格式说明符吗?

I mean something like in that pseudo code:我的意思是类似于那个伪代码:

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

which would print:这将打印:

true

There is no format specifier for bool types. bool类型没有格式说明符。 However, since any integral type shorter than int is promoted to int when passed down to printf() 's variadic arguments, you can use %d :但是,由于任何比int短的整数类型在传递给printf()的可变参数时都会提升为int ,因此您可以使用%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?反而?

There is no format specifier for bool. bool 没有格式说明符。 You can print it using some of the existing specifiers for printing integral types or do something more fancy:您可以使用一些现有的说明符来打印它来打印整数类型或做一些更花哨的事情:

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

ANSI C99/C11 don't include an extra printf conversion specifier for bool . ANSI C99/C11 不包括用于bool的额外 printf 转换说明符。

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)

In the tradition of itoa() :itoa()的传统中:

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

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

You can't, but you can print 0 or 1你不能,但你可以打印 0 或 1

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

source 来源

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));

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;

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM