Question

C printf Format Specifier for bool: How to Print Boolean Values

cbooleanprintf

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:

  • 0 means false
  • 1 means 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:

  • Print it as a number using %d
  • Print it as text using the conditional operator and %s

This matters in real programming because booleans appear everywhere: flags, validation results, function return values, feature toggles, and state checks. Knowing how to print them correctly helps with debugging, logging, and building readable output.

Mental Model

Think of bool in C as a very small integer box that can only hold two meaningful states:

  • 0 for "no"
  • 1 for "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.

Take Quiz

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 <stdbool.h>

int main(void) {
    bool is_ready = true;
    printf("%s\n", is_ready ? "true" : "false");
    return 0;
}

Output:

true

This is the most common way to get readable boolean output.

Example with both values

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

int main(void) {
    bool a = true;
    bool b = false;

    printf("a = %d, %s\n", a, a ? "true" : "false");
    printf("b = %d, %s\n", b, b ? "true" : "false");

    return 0;
}

Output:

a = 1, true
b = 0, false

This is useful for debugging because it shows both the numeric and human-readable forms.

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

  1. #include <stdbool.h> makes bool, true, and false available.
  2. bool x = true; creates a boolean variable named x.
  3. Internally, true is stored as 1.
  4. In x ? "true" : "false", C checks whether x is nonzero.
  5. Since x is true, the expression becomes "true".
  6. printf("%s\n", ...) prints that string.
  7. The final output is:
true

Trace with false

bool x = false;
printf("%s\n", x ? "true" : "false");
  • x stores 0
  • The condition is false
  • The expression becomes "false"
  • printf prints false

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 0/1 is common for compact logs, while true/false is better for readability.

Take Quiz

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));
    return 0;
}

This keeps logging consistent across the codebase.

4. Guard and validation messages

if (!is_config_valid) {
    printf("config valid: %s\n", is_config_valid ? "true" : "false");
    return 1;
}

This pattern helps explain why execution stops early.

5. Error handling and status reporting

bool success = initialize_system();
printf("initialization success: %s\n", success ? "true" : "false");

This is especially common in command-line tools and system utilities.

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 %b or %B for printf
  • 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

Broken code:

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

Why it is wrong:

  • %s expects a char * pointing to a string
  • x is not a string pointer
  • This can cause undefined behavior

Correct version:

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

Mistake 3: Forgetting stdbool.h

Broken code:

#include <stdio.h>

int main(void) {
    bool x = true;
    printf("%d\n", x);
    return 0;
}

Why it is wrong:

  • In C, bool, true, and false come from stdbool.h

Correct version:

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

Mistake 4: Expecting assigned nonzero values to stay unchanged

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

int main(void) {
    bool x = 42;
    printf("%d\n", x);
    return 0;
}

Output:

1

Why:

  • _Bool converts any nonzero value to 1
  • It does not keep the original number

Mistake 5: Using the wrong format for readability

printf("logged_in = %d\n", logged_in);

This is valid, but sometimes less clear than:

printf("logged_in = %s\n", logged_in ? "true" : "false");

Choose the style that matches your logging needs.

Comparisons

GoalApproachExampleResultBest for
Print raw boolean value%dprintf("%d\n", x);0 or 1Quick debugging
Print readable text%s with conditional operatorprintf("%s\n", x ? "true" : "false");true or falseLogs and user-facing output
Reuse formatting logicHelper functionprintf("%s\n", bool_str(x));true or falseLarger codebases

bool vs int in C

TypePurposePrinted withNotes
boolLogical true/false state%d or %s via conversionComes from stdbool.h
intGeneral integer values%dCan hold many values, not just true/false

0/1 vs true/false

Output styleProsCons
0/1Short, simple, easy for quick debug outputLess readable to humans
true/falseClear and self-explanatoryRequires conditional conversion or helper function
Take Quiz

Cheat Sheet

Quick facts

  • Standard C has no printf format specifier for bool
  • bool comes from #include <stdbool.h>
  • bool is an alias for _Bool
  • _Bool stores 0 or 1
  • 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) {
    return value ? "true" : "false";
}

Use it like this:

printf("%s\n", bool_str(x));

Things to avoid

Do not do this:

printf("%B\n", x);
printf("%b\n", x);
printf("%s\n", x);

Include headers

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

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?

Because _Bool converts any nonzero value to 1.

Do I need stdbool.h to use bool?

Yes, in C you normally include stdbool.h to use bool, true, and false.

Is _Bool different from bool?

_Bool is the built-in type. bool is a convenient alias provided by stdbool.h.

Should I print booleans as 0/1 or true/false?

Use 0/1 for quick debugging and compact output. Use true/false when readability matters.

Take Quiz

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 bool variables with different values.
  • Print each variable once as a number and once as text.
  • Use stdbool.h and printf correctly.
  • Add a helper function to convert bool to true or false.
Take Quiz

Keep learning

Related questions

View more