What is the printf format specifier for bool?
There is no format specifier for bool
types. However, since any integral type shorter than int
is promoted to int
when passed down to printf()
's variadic arguments, you can use %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?
printf conversion specifier for _Bool?
There is no specific conversion length modifier for _Bool
type.
_Bool
is an unsigned integer type large enough to store the values 0
and 1
. You can print a _Bool
this way:
_Bool b = 1;
printf("%d\n", b);
Because of the integer promotions rules, _Bool
is guaranteed to promote to int
.
What format specifier should be used for BOOL?
BOOL var = YES;
NSLog(@"var = %@", (var ? @"YES" : @"NO"));
BOOL
is merely an alias (typedef) for signed char
.
The specifiers supported by NSLog are documented here.
Is there a format specifier that works with Boolean values?
Here are two things that work:
NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO");
or you can cast:
NSLog(@"You got: %d", (int)booleanValue);
Which will output 0 or 1
Format specifier in scanf for bool datatype in C
There is none.
Use a temp object as the size of _Bool
is implementation dependent.
#include <stdbool.h>
#include <stdio.h>
bool b;
int temp;
scanf("%d", &temp);
b = temp;
Why does neither C nor Objective-C have a format specifier for Boolean values?
Pure C (back to the K&R variety) doesn't actually have a boolean type, which is the fundamental reason why native printf
and cousins don't have a native boolean format specifier. Expressions can evaluate to zero or nonzero integral values, which is what is interpreted in if
statements as false or true, respectively in C. (Understanding this is the key to understand the semantics of the delightful !!
"bang bang operator" syntax.)
(C99 did add a _Bool
type, though unless you're using purest C you're unlikely to need it; derived languages and common platforms already have common boolean types or typedefs.)
The BOOL
type is an ObjC construct, and -[NSString stringWithFormat:]
(and NSLog
) simply doesn't have an additional format specifier that does anything special with it. It certainly could (in addition to %@
), and choose some reasonable string to drop in there; I don't know whether such a thing was ever considered, but it strikes me anyway as different in kind from all other format specifiers. How would you know to appropriately localize or capitalize the string representations of "yes" or "no" (or "true" or "false"?) etc? No other format specifier results in the library making decisions like that; all others are precisely numeric or insert the string result of another method call. It seems onerous, but making you choose what text you actually want in there is probably the most elegant solution.
Related Topics
Why Do Compilers Allow String Literals Not to Be Const
When and How to Use Exception Handling
How to Check What Shared Libraries Are Loaded at Run Time for a Given Process
How to Correctly and Standardly Compare Floats
How to Store Extremely Large Numbers
Why Is the Sprite Not Rendering in Opengl
Multiple Definition of Template Specialization When Using Different Objects
What's the Meaning of * and & When Applied to Variable Names
Clarification Needed Regarding Getchar() and Newline
How to Use Std::String in a Constexpr
Should I Include <Xxxx.H> or <Cxxxx> in C++ Programs
Implementing a No-Op Std::Ostream
What Is a Copy Constructor in C++
What Is the Meaning of "... ..." Token? I.E. Double Ellipsis Operator on Parameter Pack