C Standard Library stdbool.h

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<stdbool.h>

What You’ll Learn

Before C99, C had no built-in bool keyword in everyday code—programmers used int with 0 and 1. <stdbool.h> (C99) adds bool, true, and false so conditions read clearly and match other languages.

01

bool

Type name.

02

true

Macro 1.

03

false

Macro 0.

04

_Bool

Under hood.

05

C99

Standard.

06

return

Functions.

Definition and Usage

bool is a macro that expands to _Bool, a type that holds only two values: 0 (false) and 1 (true). Assigning any non-zero integer to a _Bool stores 1.

true and false are macros for integer constants 1 and 0. Use them for initialization and return values so intent is obvious. In if conditions, write if (flag) rather than if (flag == true).

💡
Beginner Tip

Include <stdbool.h> in every file that uses bool. The header also defines __bool_true_false_are_defined so headers can detect that true/false are available.

📝 Syntax

Include the header:

C
#include <stdbool.h>

What stdbool.h defines

  • bool — macro for _Bool.
  • true — macro for 1.
  • false — macro for 0.
  • __bool_true_false_are_defined — feature-test macro (C99).

Common usage

C
bool ready = false;
bool is_valid = true;

bool check(int x) {
    return x > 0;   /* expression converts to 0 or 1 */
}

if (ready) { /* ... */ }
if (!is_valid) { /* ... */ }

Headers and linking

  • #include <stdbool.h> — macros only, no link flag.
  • Compile: gcc program.c -std=c11 -o program (C99 or later).
  • C++ has built-in bool; do not assume C++ headers define C bool the same way in mixed code.

⚡ Quick Reference

SymbolExpands toMeaning
bool_BoolBoolean type
true1True value
false0False value
if (b)Test true
if (!b)Test false
Declare
bool flag = false;

Initialize

Return
return n % 2 == 0;

Predicate

Print
b ? "yes" : "no"

As text

Toggle
on = !on;

Flip flag

Examples Gallery

Compile with gcc file.c -std=c11 -o out. Every example needs #include <stdbool.h>.

📚 Getting Started

Boolean functions and conditions from the reference.

Example 1 — is_even with bool Return Type

From the reference: a function that returns true or false.

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

bool is_even(int number) {
    return (number % 2) == 0;
}

int main(void) {
    int num = 4;

    if (is_even(num)) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }

    printf("is_even(7) stores as int value: %d\n", is_even(7));

    return 0;
}

How It Works

(number % 2) == 0 is a comparison expression; its result is 0 or 1, which _Bool stores. Use if (is_even(num)) directly—no need for == true.

Example 2 — Validation with Early Return

Boolean helpers make input checks readable.

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

bool is_positive(int x) {
    return x > 0;
}

int main(void) {
    int values[] = { -3, 0, 42 };
    size_t i;

    for (i = 0; i < 3; i++) {
        if (is_positive(values[i])) {
            printf("%d is positive\n", values[i]);
        } else {
            printf("%d is not positive\n", values[i]);
        }
    }

    return 0;
}

How It Works

Zero is not positive—is_positive(0) returns false. Naming functions is_* signals they return bool.

📈 Practical Patterns

Flags, toggles, and storage rules.

Example 3 — Toggle a bool Flag

Common pattern for on/off state.

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

int main(void) {
    bool lights_on = false;

    printf("Start: %s\n", lights_on ? "on" : "off");

    lights_on = true;
    printf("After set true: %s\n", lights_on ? "on" : "off");

    lights_on = !lights_on;
    printf("After toggle: %s\n", lights_on ? "on" : "off");

    return 0;
}

How It Works

!lights_on flips between 0 and 1. The ternary ? : prints friendly text; printf("%d", lights_on) would show 0 or 1.

Example 4 — Array of Feature Flags

Track multiple yes/no options in one struct-like array.

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

enum { FLAG_WIFI, FLAG_BLUETOOTH, FLAG_GPS, FLAG_COUNT };

int main(void) {
    bool features[FLAG_COUNT] = { true, false, true };
    const char *names[] = { "WiFi", "Bluetooth", "GPS" };
    size_t i;

    for (i = 0; i < FLAG_COUNT; i++) {
        printf("%s: %s\n", names[i], features[i] ? "enabled" : "disabled");
    }

    return 0;
}

How It Works

bool arrays work like other small integer types. Each element is 0 or 1. For many flags, a bit-field or unsigned int mask can save memory—bool is clearest for beginners.

Example 5 — Non-zero Becomes true (Normalization)

Assigning 42 to a bool stores 1, not 42.

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

int main(void) {
    bool flag;

    flag = 0;
    printf("assign 0  -> flag = %d\n", flag);

    flag = 42;
    printf("assign 42 -> flag = %d\n", flag);

    flag = -1;
    printf("assign -1 -> flag = %d\n", flag);

    return 0;
}

How It Works

_Bool normalizes values: only 0 is false, everything else becomes 1. That matches how if (x) treats integers in C.

🚀 Common Use Cases

  • Predicate functionsis_valid, has_permission, contains.
  • State flagsbool connected, bool debug_mode.
  • Loop conditionswhile (running) with bool running.
  • Search results — return true when an item is found.
  • Config options — enable/disable features in settings structs.
  • Readability — clearer than magic int 0/1 in modern C99+ code.

🧠 How stdbool.h Works

1

Include header

bool, true, false macros become available.

Setup
2

Store 0 or 1

_Bool variables hold normalized true/false.

Storage
3

Use in conditions

if (flag) branches on zero vs non-zero.

Branch
=

Readable logic

Code expresses yes/no intent without custom typedefs.

📝 Notes

  • C99 feature—use -std=c99, c11, or newer.
  • bool promotes to int in expressions like other small integers.
  • sizeof(bool) is typically 1 byte but is implementation-defined.
  • Do not redefine true/false in your own headers.
  • printf("%d", flag) prints 0 or 1; there is no %b in standard C.
  • In C++, bool is a keyword—no need for stdbool.h in C++ files.

⚡ Optimization

bool is as cheap as a small integer. For huge arrays of flags, bit-packing into unsigned int masks saves memory but adds complexity. For clarity and typical programs, bool is fine.

Conclusion

<stdbool.h> brings readable boolean types to C99+. Use bool for variables and return types, true and false for literals, and if (flag) for tests.

Remember normalization: only 0 is false. Include the header in every translation unit that uses these names.

💡 Best Practices

✅ Do

  • #include <stdbool.h> when using bool
  • Name predicates is_* or has_*
  • Initialize: bool ok = false;
  • Use if (flag) and if (!flag)
  • Return comparison expressions from bool functions

❌ Don’t

  • Write if (flag == true) (redundant)
  • Define your own TRUE/FALSE macros
  • Assume bool exists in C89 without the header
  • Store non 0/1 values expecting them to round-trip
  • Mix C stdbool.h blindly into C++ translation units

Key Takeaways

Knowledge Unlocked

Five things to remember about stdbool.h

Booleans in C, explained simply.

5
Core concepts
📚 02

true

Is 1.

Macro
📈 03

false

Is 0.

Macro
📄 04

C99

Need -std.

Standard
🌐 05

if (b)

Idiom.

Style

❓ Frequently Asked Questions

stdbool.h is a C99 header that defines bool as a macro for _Bool, plus true and false as macros for 1 and 0. It gives C a readable boolean type for conditions, flags, and function return values.
_Bool is the built-in keyword type (C99). bool is a macro in stdbool.h that expands to _Bool. In portable code, include stdbool.h and write bool—not _Bool directly—unless you have a special reason.
Yes. stdbool.h was added in C99. Compile with gcc -std=c99 or -std=c11. Older C89 compilers do not provide it; those programs used int with 0 and 1 instead.
Usually no. Idiomatic C uses if (flag) and if (!flag). Comparing to true is redundant. The old reference suggested flag == true for clarity—that is uncommon in real C code and can confuse readers.
You can, but _Bool stores only 0 or 1. Any non-zero value becomes 1 (true). When reading the variable later you get 0 or 1, not the original 5.
Not portably for bool/true/false names. You could use _Bool with literal 1/0, or int before C99. Always #include <stdbool.h> when you want the standard bool, true, and false macros.
Did you know?

C99 added _Bool to the language and put the friendly names in stdbool.h so existing code using macros named bool would not break. That is why the keyword is _Bool but everyday code says bool after including the header.

Explore C Standard Library Headers

Continue with stddef.h or browse the library index.

Standard Library Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful