Skip to content
VLSI Mentor

C Part I · Module 2 · Values, Variables, Types and Representation

Boolean Values in C

Truth in C works in a way that looks eccentric until you know that the language spent most of its life without a dedicated type for it. The underlying model is simple and is still what everything rests on: zero means false, and any other value means true. A proper bool type exists now and is worth using, but it is a layer added on top of that model rather than a replacement for it — which is why storing 42 into a bool does not give you an object holding 42. This is also the clearest place in the curriculum to see the C17 and C23 difference that Chapter 1.3 promised would be named where it matters, because it changes the first line of every example here.

Foundation8 min readCPart IFundamentalsStandards

Part I · Module 2 · Chapter 2.10 · Boolean Values in C

1. The Model Underneath

C decides truth by one rule, and it applies to every value you will ever test:

Zero is false. Every other value is true.

Not "1 is true". Every non-zero value — 42, −7, 0.5, any of them. There is nothing more to the model than that.

This is older than it looks. C had no boolean type at all for its first two decades, so conditions were written using ordinary integers and the convention above. That history is why the rule is phrased around zero rather than around a pair of named values, and why it still governs everything today.

You have not yet met the constructs that test a condition — if and the loops are Module 4's — so this chapter talks about conditions without writing one. What matters now is the rule itself, because Module 4 assumes it completely.

2. Comparisons Produce 1 and 0

A comparison in C does not produce a special truth object. It produces an ordinary integer: 1 if the comparison holds, 0 if it does not.

Azvya Education Pvt. Ltd.VLSI Mentor
a comparison is a value
#include <stdio.h>

int main(void)
{
    int score = 42;
    int zero = 0;

    printf("42 is not zero: %d\n", score != 0);
    printf("0  is not zero: %d\n", zero != 0);
    return 0;
}

Output:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
42 is not zero: 1
0  is not zero: 0

score != 0 is an expression like any other, and its value is 1. You can print it, store it, or pass it around. Module 3.5 covers the comparison operators properly; the point here is only that their results are values, not a separate category of thing.

3. What bool Adds

C eventually gained a dedicated type, and it is worth using — code that says bool states its intent in a way int cannot.

Azvya Education Pvt. Ltd.VLSI Mentor
declaring a truth value
#include <stdbool.h>
#include <stdio.h>

int main(void)
{
    int score = 42;
    bool ready = true;

    printf("ready holds %d\n", ready);

    ready = score;
    printf("after storing 42, ready holds %d\n", ready);
    return 0;
}

Output:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
ready holds 1
after storing 42, ready holds 1

The second line is the interesting one. ready = score; stored 42 into a bool, and the object ended up holding 1, not 42.

That is not printf displaying it oddly. A bool can hold only 0 or 1, so converting any other value to it applies §1's rule: non-zero becomes 1. The conversion is part of the assignment and the original value is gone.

So bool is a real type with real behaviour, not a decorative name for int. It narrows what the object can hold, which is exactly what makes it worth using: an object declared bool cannot quietly end up holding 42.

4. The C17 and C23 Difference

Notice the #include <stdbool.h> at the top of §3's program. It is doing real work.

On this curriculum's C17 baseline, bool, true and false are not keywords. They are supplied by the header <stdbool.h>, and without that line the program does not compile:

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
error: use of undeclared identifier 'bool'

In C23, bool, true and false became keywords in the language itself. The header still exists so that older code keeps working, but it is no longer needed.

5. What You Should Be Able to Say Now

  1. Zero is false; every other value is true. That is C's whole truth model, and it predates the bool type.
  2. A comparison produces an ordinary integer — 1 or 0 — not a special truth object.
  3. bool is a real type that can hold only 0 or 1.
  4. Storing a non-zero value into a bool converts it to 1. The original value does not survive.
  5. That narrowing is the reason to prefer bool over int for truth values.
  6. Under C17, bool, true and false come from <stdbool.h>. Under C23 they are keywords.
  7. Including the header is correct under both, so include it.

6. Exercise

Exercise 1 — apply the rule. For each value, say whether C treats it as true or false: 1, 0, -1, 42, 0.0, 0.1.

Exercise 2 — predict. What does each line print?

Azvya Education Pvt. Ltd.VLSI Mentor
Snippet
bool flag = 7;
printf("%d\n", flag);
printf("%d\n", 5 != 5);
printf("%d\n", 5 != 4);

Exercise 3 — explain the loss. In §3, ready = score; left ready holding 1 rather than 42. Explain in two sentences what happened, and say whether the same would happen if ready had been declared int.

Exercise 4 — diagnose the build. A colleague's C17 file uses bool and fails to compile with use of undeclared identifier. They insist the same file compiles for them. Give the most likely explanation and the one-line fix.

Exercise 5 — argue the type. A function needs to record whether a check passed. Give one concrete reason to declare that object bool rather than int, based on §3 rather than on style.

7. Summary

  • Zero is false, everything else is true. One rule, older than C's boolean type, and still what Module 4 will rely on.
  • Comparisons yield 1 or 0 — ordinary integer values you can store and print.
  • bool holds only 0 or 1. Storing any other non-zero value converts it to 1, and the original is gone.
  • That narrowing is the point. A bool cannot quietly hold 42; an int can.
  • C17 needs <stdbool.h>; C23 does not. Including it is correct under both, so include it.

This module has now covered the kinds of value C works with. What it has not examined is where values in your source code come from in the first place — 3, 0xFF, 'A' and 0.1 have all appeared without anyone saying what kind of thing they are — or how to declare an object whose value must not change afterwards. That is the next chapter.

Standards & specifications

Governing standard
ISO/IEC 9899 (the C language standard)(opens ISO/IEC (JTC 1/SC 22/WG 14) in a new tab)

Defines the C language itself — the type system, expression semantics, object model, storage duration and the behaviour categories a conforming implementation must honour. It is a FAMILY of editions (C89/C90, C99, C11, C17, C23) and this curriculum names the edition wherever a claim depends on one. It does not define any foreign-function interface to a hardware description language, and it does not define platform behaviour: what a memory-mapped access does on real silicon is the implementation's and the device's contract, not this standard's. Working drafts published by WG14 are freely available and are what most engineers read day to day; a draft is not the published standard.

This page also covers RTL structure, verification approach and debugging technique. Those are engineering practice built on the standard, not requirements the standard itself imposes.

Where this fits

Part of the C Programming curriculum.