liveBook · Manning

Boolean

As explained in Modern C, Third Edition

bool is a C type whose values represent logical truth and falsity.

In C, bool stores logical truth values: false is 0 and true is 1. Use these names when a value is intended to act as a condition rather than comparing explicitly with numeric constants. section 3.1 section 5.7.4

Remember these rules:

  • A numerical condition is false only when its value is 0; every nonzero value is true. section 3.1
  • A bool object has only the values false and true, corresponding to 0 and 1. section 5.7.4
  • Do not write values other than 0 or 1 into a bool object or modify its representation bytes directly. section 15.1.6

Use a truth value directly

bool b = ...; if (b) { /* execute when b is true */ } This is clearer than testing ((b != false) == true). Likewise, a numerical variable can be used directly when zero means false and any nonzero value means true. section 3.1

for (size_t i = 0; i < 5; ++i) {
  if (i) {
    printf("element %zu is %g, \tits square is %g\n",
           i,
           A[i],
           A[i]*A[i]);
  }
}

A loop demonstrates a nonzero index used directly as a condition; the body therefore skips the zero index. section 3.1

How Modern C, Third Edition covers it

How deep it goes
Defines bool values and representation constraints, but defers broader type details to the Boolean data type treatment.
Examples
Demonstrates direct bool conditions, nonzero numerical conditions, and a loop that skips index zero.
What it assumes
Assumes familiarity with C variables, expressions, assignments, and if statements; teaches Boolean values from their conditional behavior.
What you can do after
Apply bool directly in conditions and avoid redundant comparisons while preserving valid Boolean representations.
How it connects
Connects bool with if statements, numerical conditions, equality operators, unsigned types, and object representations.

Also explained in

Compare how 11 books explain boolean →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.