What is the printf format specifier for bool?

前端 未结 8 633
有刺的猬
有刺的猬 2020-11-28 17:13

Since ANSI C99 there is _Bool or bool via stdbool.h. But is there also a printf format specifier for bool?

I mean

相关标签:
8条回答
  • 2020-11-28 18:03

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

    #include <ios>
    #include <iostream>
    
    bool b = IsSomethingTrue();
    std::cout << std::boolalpha << b;
    
    0 讨论(0)
  • 2020-11-28 18:14

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

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

    Especially useful with Flags:

    #define MY_FLAG (1 << 4)
    int flags = MY_FLAG;
    printf("%d\n", !!(flags & MY_FLAG));
    
    0 讨论(0)
提交回复
热议问题