C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
116: Boolean Type with stdbool.h
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, andis_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 returnstrueonly 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.
There are no comments for now.