Question
In C, since the C99 standard introduced _Bool and bool through stdbool.h, is there a dedicated printf format specifier for boolean values?
For example, is something like this valid:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool x = true;
printf("%B\n", x);
return 0;
}
with the expectation that it would print:
true
If not, what is the correct way to print a bool in C?
Short Answer
By the end of this page, you will understand that C does not provide a dedicated printf format specifier for bool. You will learn why this is the case, how bool behaves in C, and the standard ways to print boolean values either as 0/1 or as the strings true/false.
Concept
In C, bool is not a special printable type with its own printf format like %d for int or %f for double.
When you include stdbool.h, the name bool becomes a convenient alias for the built-in integer type _Bool. A _Bool stores logical values:
0means false1means true
Any nonzero value assigned to _Bool is converted to 1.
printf works by reading arguments according to the format specifiers you provide. Since the C standard does not define a boolean format specifier such as %b or %B for printf, you must print a bool using one of the existing supported forms.
The two most common choices are:
Mental Model
Think of bool in C as a very small integer box that can only hold two meaningful states:
0for "no"1for "yes"
printf is like a label printer. It only knows how to print boxes when you tell it the right label format.
There is no built-in "boolean label" in standard C. So if you hand it a bool, you must choose one of these labels yourself:
- Print the raw number:
%d - Convert it to a word first:
x ? "true" : "false"
So the main idea is: C stores booleans simply, but you must decide how you want them displayed.
Syntax and Examples
Print a bool as 0 or 1
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool is_ready = true;
printf("%d\n", is_ready);
return 0;
}
Output:
1
This works because bool is really _Bool, which behaves like an integer type in this context.
Print a bool as true or false
#include <stdio.h>
#include
{
is_ready = ;
(, is_ready ? : );
;
}
Step by Step Execution
Consider this example:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool x = true;
printf("%s\n", x ? "true" : "false");
return 0;
}
Step-by-step
#include <stdbool.h>makesbool,true, andfalseavailable.bool x = true;creates a boolean variable namedx.- Internally,
trueis stored as1. - In
x ? "true" : "false", C checks whetherxis nonzero. - Since
xis true, the expression becomes"true".
Real World Use Cases
Boolean printing is common in many practical situations:
Debugging program state
printf("connected: %s\n", connected ? "true" : "false");
Useful when checking why a program is taking one path and not another.
Logging feature flags
printf("dark_mode enabled: %s\n", dark_mode ? "true" : "false");
Applications often use boolean flags to turn features on or off.
Reporting validation results
printf("input valid: %s\n", is_valid ? "true" : "false");
Helpful in scripts, command-line tools, and parsers.
Testing and diagnostics
printf("test passed = %d\n", passed);
In quick debug output, 0 and 1 may be enough.
Embedded and systems programming
In low-level C code, booleans often represent hardware states, configuration bits, or success/failure checks. Printing is common for compact logs, while is better for readability.
Real Codebase Usage
In real codebases, developers usually print booleans in one of these patterns:
1. Human-readable logs
printf("cache enabled: %s\n", cache_enabled ? "true" : "false");
This is common in logs and diagnostics because it is easy to read.
2. Compact numeric output
printf("retry=%d timeout=%d\n", retry_enabled, timeout_enabled);
Useful in quick debugging or machine-readable output.
3. Helper macro or helper function
When a project prints booleans often, teams sometimes define a reusable helper.
#include <stdio.h>
#include <stdbool.h>
const char *bool_str(bool value) {
return value ? "true" : "false";
}
int main(void) {
bool ok = false;
printf("ok = %s\n", bool_str(ok));
;
}
Common Mistakes
Mistake 1: Assuming %b or %B exists in standard C
Broken code:
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool x = true;
printf("%B\n", x);
return 0;
}
Why it is wrong:
- Standard C does not define
%bor%Bforprintf - The behavior is not portable
- Some compilers may reject it or produce unexpected output
Use this instead:
printf("%d\n", x);
printf("%s\n", x ? "true" : "false");
Mistake 2: Using %s directly with a bool
Comparisons
| Goal | Approach | Example | Result | Best for |
|---|---|---|---|---|
| Print raw boolean value | %d | printf("%d\n", x); | 0 or 1 | Quick debugging |
| Print readable text | %s with conditional operator | printf("%s\n", x ? "true" : "false"); | true or false | Logs and user-facing output |
| Reuse formatting logic | Helper function | printf("%s\n", bool_str(x)); |
Cheat Sheet
Quick facts
- Standard C has no
printfformat specifier forbool boolcomes from#include <stdbool.h>boolis an alias for_Bool_Boolstores0or1- Any nonzero assigned value becomes
1
Common ways to print a boolean
Print as a number:
printf("%d\n", x);
Print as text:
printf("%s\n", x ? "true" : "false");
Reusable helper
const char *bool_str(bool value) {
value ? : ;
}
FAQ
Is there a %b or %B format specifier for bool in C?
No. Standard C does not define %b or %B for printf boolean output.
How do I print true or false in C?
Use %s with a conditional expression:
printf("%s\n", x ? "true" : "false");
Can I print a bool with %d?
Yes. A bool in C is based on _Bool, so %d prints it as 0 or 1.
Why does bool x = 42; print 1?
Mini Project
Description
Create a small C program that prints the status of several application flags, such as whether a user is logged in, whether debugging is enabled, and whether the network is connected. This demonstrates both common ways to print booleans: numeric output and readable text output.
Goal
Build a C program that displays boolean values as both 0/1 and true/false.
Requirements
- Define at least three
boolvariables with different values. - Print each variable once as a number and once as text.
- Use
stdbool.handprintfcorrectly. - Add a helper function to convert
booltotrueorfalse.
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
Calling C or C++ from Python: Building Python Bindings
Learn the quickest ways to call C or C++ from Python, including ctypes, C extensions, Cython, and binding tools with practical examples.
Convert char to int in C and C++
Learn how to convert a char to an int in C and C++, including character codes, digit conversion, examples, mistakes, and practical usage.