C stdbool.h Header

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

What Is stdbool.h?

<stdbool.h> is a C99 header that defines bool, true, and false for readable yes/no values. Under the hood, bool is _Bool, and values normalize to 0 or 1.

Remember
#include <stdbool.h>

bool flag = true;    // or false
if (flag) { ... }    // idiomatic — not flag == true

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

true  → 1
false → 0
any non-zero assign → stored as 1
UsePrefer
Yes/no flags and returnsbool / true / false
Built-in type name_Bool (usually via bool)
Pre-C99 styleint with 0 / 1

How to Use stdbool.h in C

Include the header, declare bool variables or returns, and test with if (flag).

  1. Add #include <stdbool.h> (compile with -std=c99 or -std=c11).
  2. Declare bool variables or make functions return bool.
  3. Assign true / false, or a comparison like (n % 2) == 0.
  4. Test with if (flag) or if (!flag)—not == true.
PieceRequired?Role
#include <stdbool.h>YesDefines bool, true, false
boolYesType for 0/1 values (_Bool)
true / falseCommonMacros for 1 and 0

Pseudocode

Pseudocode
include stdbool.h

function is_even(n):
    return (n % 2) == 0     // true or false

flag = false
flag = true
flag = !flag                // toggle

if flag:
    // do work
else:
    // other path

Cheat Sheet

Keep these facts handy while writing boolean code.

TopicDetail
Header#include <stdbool.h> (C99+)
Typebool → _Bool
Constantstrue = 1, false = 0
Testif (flag) / if (!flag)
Toggleflag = !flag;
NormalizeAny non-zero assignment → stored as 1
Sample checkis_even(4) → true; is_even(7) → false

Live Preview

Simulate common bool operations: even check, toggle, and non-zero normalization.

Browser simulation of bool behavior (same idea as the C examples below).

Result
is_even(4)
→ (4 % 2) == 0
→ true (1)

4 is even.

Worked Walkthrough — is_even(4)

Trace a boolean helper: compare, store as bool, then branch with if.

Calculation
is_even(4):
  (4 % 2) == 0
  → 0 == 0
  → true (1)

main:
  if (is_even(4))   // condition is true
      print "4 is even."

is_even(7):
  (7 % 2) == 0
  → 1 == 0
  → false (0) ✓

C Examples

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

Example 1 — Return bool from is_even

A small helper that returns true or false for an even check.

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) as int value: %d\n", is_even(7));

    return 0;
}

How It Works

  1. Include the header. stdbool.h brings in bool, true, and false.
  2. Return a comparison. (number % 2) == 0 is already 0 or 1—perfect for bool.
  3. Test directly. if (is_even(num)) is idiomatic; skip == true.
  4. Print as number. With %d, false shows as 0 (and true as 1).

Example 2 — Toggle a bool Flag

Common on/off pattern using assignment and !.

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

  1. Start false. lights_on = false means off (0).
  2. Set true. Assigning true turns the flag on.
  3. Flip with !. lights_on = !lights_on swaps 0 ↔ 1.
  4. Print friendly text. The ternary chooses "on" / "off" from the boolean.

Example 3 — 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

  1. Zero stays false. flag = 0 stores 0.
  2. Any non-zero becomes 1. 42 and -1 both store as true.
  3. Original value is lost. You cannot recover 42 from the bool later.
  4. Prefer true/false. Assign true/false (or comparisons) instead of magic integers.

Edge Cases & Pitfalls

Check these before relying on bool in real projects.

style

Avoid == true

Write if (flag) and if (!flag). Comparing to true is redundant in idiomatic C.

normalize

Non-zero collapses to 1

Do not store counters or error codes in a bool—only yes/no information survives.

standard

Needs C99+

Compile with -std=c99 or -std=c11. C89 code used int flags instead.

include

Include the header

Without <stdbool.h>, the names bool, true, and false are not portable.

Performance Notes

ApproachCostBest for
bool / _BoolTiny (usually 1 byte)Flags and return values
int 0/1Often 4 bytesLegacy C89 code
Bit masksPacked bitsMany flags in tight structs

Prefer bool for clarity. Switch to bit fields or masks only when packing many flags matters for size or ABI.

Key Takeaways

  • Role: readable yes/no type via bool, true, false.
  • Storage: always 0 or 1 after assignment.
  • Style: if (flag), not if (flag == true).
  • Standard: C99+ with #include <stdbool.h>.

One line: stdbool.h gives C a clear boolean vocabulary—still just 0 and 1 under the hood.

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 and uncommon in real C code.
You can, but _Bool stores only 0 or 1. Any non-zero value becomes 1 (true). When you read the variable later you get 0 or 1, not the original 5.
Not portably for the names bool, true, and false. You could use _Bool with literal 1/0, or int before C99. Always #include <stdbool.h> when you want the standard macros.

Next: stddef.h

Learn size_t, ptrdiff_t, NULL, and other common typedefs.

stddef.h tutorial →

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.

9 people found this page helpful