Skip to Content
Course content

116: Boolean Type with stdbool.h

Click on the "Edit" button in the top corner of the screen to edit your slide content.

A few years ago, I was reviewing code for a junior developer working on a network packet parser. He had used a standard int to track whether a packet was encrypted, naming the variable is_encrypted. In one part of the code, he checked if (is_encrypted), which worked fine. But in another, he wrote if (is_encrypted == 1). Later, another teammate changed the logic to use that same variable as a bitmask, where 2 meant "encrypted with AES" and 1 meant "encrypted with DES". Suddenly, the check for == 1 started failing for AES packets, even though the packet was technically encrypted. It was a classic "truthy" bug that took us half a day to track down.

The Ambiguity of Integer Flags

In the early days of C, there was no dedicated boolean type. We just used integers: 0 for false, and anything else for true. While this is powerful, it's dangerous because it mixes intent with value. When you see int, you don't know if the variable is meant to be a counter, a status code, or a simple yes/no toggle. If you use int for a boolean, you're leaving the door open for someone (including your future self) to accidentally store a 2 or a -1 in there, which can lead to the exact kind of logical fragility I mentioned in that packet parser story.

Bringing in stdbool.h

Since C99, we've had a much better way to handle this: the <stdbool.h> header. When you include this, you get access to the bool type, along with the constants true and false. Technically, bool is a macro for the built-in type _Bool, but you should almost always use bool for the sake of readability.

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

bool is_system_ready = false;

int main() {
    is_system_ready = true;

    if (is_system_ready) {
        printf("Systems are go!\n");
    }
    return 0;
}

Using bool doesn't fundamentally change how the CPU processes the logic—under the hood, it's still basically an integer—but it changes how the programmer reads the code. It signals a strict contract: this variable can only ever be one of two states.

Truthiness vs. The Bool Type

It is important to remember that C still treats integers as booleans in conditional statements. You can still pass an int into an if statement, and C will evaluate 0 as false and non-zero as true. However, using stdbool.h allows you to be explicit about your data types.

I always recommend using bool for function return types that are meant to indicate success or failure. Compare these two function signatures:

  • int check_connection(); (Does this return a boolean? An error code? The number of connected peers?)
  • bool check_connection(); (This is crystal clear: it's either connected or it isn't.)

By using the correct type, you eliminate the need for comments explaining what the return value means, because the type itself is the documentation.




📋 Practical Task

Building a Server Health Monitor

Your task is to create a small program that simulates a server health check. Instead of using integers, use <stdbool.h> to manage the state of the server components.

Requirements:

  • Create three boolean variables: is_cpu_overloaded, is_disk_full, and is_network_up.
  • Initialize them to represent a "warning" state (e.g., CPU is overloaded, disk is not full, network is up).
  • Write a function bool is_server_healthy(bool cpu, bool disk, bool net) that returns true only if the CPU is NOT overloaded, the disk is NOT full, and the network IS up.
  • In main, call this function and print "Server Status: OK" or "Server Status: CRITICAL" based on the result.
  • Change one of the boolean values and run the check again to verify the status changes correctly.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.