Course contentsShow
← C
Lesson 966 of 1,21913. Advanced CFree lesson

C99: Variable declarations and `bool`

C99: Variable declarations and bool

What you'll learn: How the C99 standard modernized C by allowing flexible variable placement and introducing a dedicated boolean type.

Before the C99 standard arrived, C was quite strict about where you could put your tools. Imagine a workshop where a rule stated you must lay out every single tool you might use on the workbench before you are allowed to start sawing or hammering. In "Old C" (C89), you had to declare all your variables at the very beginning of a function or a block. If you realized you needed a counter halfway through a long function, you had to scroll all the way back up to the top to add it.

Flexible declarations

C99 changed the game by allowing "mixed declarations." This means you can declare a variable right at the moment you need it. This is a huge win for readability. If a variable is only used in the last three lines of a 50-line function, declaring it on line 47 keeps its purpose fresh in your mind and prevents "variable pollution" at the top of your code.

This change also gave us the modern for loop syntax: for (int i = 0; i < 10; i++). In the old days, that int i had to be declared outside the loop!

The arrival of bool

Surprisingly, early C didn't have a true "True" or "False" type. Programmers just used integers, where 0 was false and anything else was true. While functional, it wasn't very clear. C99 introduced the <stdbool.h> header, which finally gave us the bool type along with the constants true and false.

Seeing it in action

Here is how these two features look when combined in a modern way:

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

int main() {
    // We can perform actions first...
    printf("Initializing system...\n");

    // ...and declare variables exactly when needed
    bool isSystemReady = true; 

    if (isSystemReady) {
        // Declaring the loop counter 'i' right inside the loop
        for (int i = 1; i <= 3; i++) {
            printf("Running check %d...\n", i);
        }
    }

    return 0;
}

By allowing variables to be declared anywhere and giving us a proper bool type, C99 made code much more intuitive and reduced the cognitive load on the programmer.

Key takeaway

C99 lets you declare variables anywhere in a block and introduced <stdbool.h> for clearer logic using true and false.