C Programming
Boolean Values
Programming Concepts
Code Implementation
Computer Science

Using Boolean values in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Boolean logic in C is simple in concept but slightly unusual in syntax because the language historically treated truth as an integer convention. Modern C gives you a real boolean type through _Bool and the convenience macros in stdbool.h, but understanding the older integer behavior is still important because both styles appear in real code.

The Modern Boolean Type in C

Since C99, the language includes _Bool, and the standard header stdbool.h defines the friendlier aliases bool, true, and false.

c
1#include <stdio.h>
2#include <stdbool.h>
3
4int main(void) {
5    bool ready = true;
6
7    if (ready) {
8        printf("ready\n");
9    } else {
10        printf("not ready\n");
11    }
12
13    return 0;
14}

This is the clearest way to express boolean intent in modern C.

Under the hood:

  • 'bool expands to _Bool'
  • 'true expands to 1'
  • 'false expands to 0'

That means the syntax looks modern, but it still fits naturally into C's integer-based semantics.

How Truth Works in C

In conditional expressions, C treats:

  • '0 as false'
  • any nonzero value as true

So all of these conditions are considered true:

c
if (5) { /* true */ }
if (-3) { /* true */ }
if (100) { /* true */ }

and this is false:

c
if (0) { /* false */ }

That rule explains why older C code often uses int for flags.

_Bool Normalizes Values

A useful detail about _Bool is that assigning any nonzero integer to it becomes 1.

c
1#include <stdio.h>
2#include <stdbool.h>
3
4int main(void) {
5    bool a = 0;
6    bool b = 42;
7    bool c = -7;
8
9    printf("%d %d %d\n", a, b, c);
10    return 0;
11}

The output is:

text
0 1 1

That normalization is one reason bool is safer and clearer than using plain integers when the value is supposed to represent only truth.

Relational and Logical Operators Return Boolean-Like Results

Comparisons and logical operators already produce values that behave like booleans.

c
1#include <stdio.h>
2#include <stdbool.h>
3
4int main(void) {
5    int x = 10;
6    int y = 20;
7
8    bool less = x < y;
9    bool both_positive = (x > 0) && (y > 0);
10
11    printf("less=%d\n", less);
12    printf("both_positive=%d\n", both_positive);
13    return 0;
14}

This is often the cleanest way to compute boolean state instead of manually assigning 0 and 1.

Boolean Flags in Functions

Using bool makes function interfaces easier to read.

c
1#include <stdbool.h>
2
3bool is_even(int value) {
4    return value % 2 == 0;
5}

Compare that to returning int and expecting the caller to remember that 0 means false and nonzero means true. Both are legal, but the boolean return type communicates intent better.

Older C Code Without stdbool.h

You will still see codebases that avoid stdbool.h, especially older or low-level projects.

c
1int done = 0;
2
3while (!done) {
4    done = 1;
5}

This works perfectly well, but it is easier to misuse because done is just an integer. For new code, bool is usually the better default unless project conventions require otherwise.

Printing and Debugging Boolean Values

C's printf does not have a special %b format for booleans. Since bool is an integer-like type, %d is commonly used.

c
printf("flag=%d\n", flag);

If you want more readable logs, map the value to strings yourself.

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

That is often easier to scan when debugging.

Common Pitfalls

A common mistake is forgetting to include stdbool.h and then trying to use bool as if it were built in. In standard C, the alias comes from the header.

Another issue is comparing boolean expressions to true or false unnecessarily. Writing if (flag) is usually cleaner than if (flag == true).

Developers also sometimes assume only 1 counts as true. In ordinary C expressions, any nonzero integer is true.

Finally, when interfacing with older APIs, remember that many functions still return int-style success flags rather than bool. Read the contract, not just the type name you wish it had.

Summary

  • Modern C uses _Bool and stdbool.h for boolean values.
  • 'bool, true, and false improve readability over raw integers.'
  • In C conditionals, zero is false and any nonzero value is true.
  • '_Bool normalizes nonzero assignments to 1.'
  • Prefer bool for new code, but understand integer truth rules for older codebases.

Course illustration
Course illustration

All Rights Reserved.