C Tutorial
C Booleans
stdbool.h gives bool, true, and false. Comparisons produce 0 or 1. Conditions consume them.
true or false
Include #include <stdbool.h> to get the type bool and the literalstrue and false. A bool stores a yes-or-no fact: a door is open, a score is high enough, a loop should keep going.
Write the literals in lowercase. True and False are Python. gcc will not compile them. Without stdbool.h, C still uses 0 for false and any non-zero value for true.
A complete program
Declare a bool, print it, then print a comparison. printf with %d shows1 and 0, not the words true and false.
Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
bool ready = true;
bool empty = false;
printf("%d\n", ready);
printf("%d\n", empty);
printf("%d\n", 7 > 3);
printf("%d\n", 7 == 3);
return 0;
}Run this in /c/try. You should see 1, 0, 1,0. That editor is gcc for C.
Comparisons produce 0 or 1
Every comparison expression has an integer result: 1 when the test holds, 0 when it does not. You can store it in a bool, print it, or pass it straight into if. The operators are the same ones you saw in Operators.
| Expression | True when |
|---|---|
a == b | a equals b |
a != b | a is not equal to b |
a < b | a is less than b |
a <= b | a is less than or equal to b |
a > b | a is greater than b |
a >= b | a is greater than or equal to b |
Use == to compare. A single = assigns. Writingif (x = 1) { ... } sets x to 1 and then treats that as true. The compiler may warn; the program is still wrong.
Combine with and, or, not
C uses && (and), || (or), and ! (not). &&is true only if both sides are true. || is true if at least one side is true. ! flips a value.
Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int age = 20;
bool member = true;
printf("%d\n", age >= 18 && member);
printf("%d\n", age < 18 || member);
printf("%d\n", !member);
return 0;
}C also uses 0 and 1
You do not have to include stdbool.h. An int holding 0 or 1works in the same places. if, while, and the middle of for treat 0 as false and every other number as true.
Example
#include <stdio.h>
int main(void) {
int ok = 1;
int empty = 0;
printf("%d\n", ok);
printf("%d\n", empty);
if (ok) {
printf("ready\n");
}
return 0;
}What to remember
#include <stdbool.h>givesbool,true, andfalse.- Comparisons such as
==and<produce 0 or 1. printfwith%dprints 1 and 0.- Combine tests with
&&,||, and!.
Next: if and else — the first place those values get used.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Chemistry
Acidic or not
On the pH scale, 7 is neutral at 25 °C. Below 7 is acidic, above 7 is alkaline. 4.2 is lemon-juice territory, not a strong mineral acid, but it is still acidic.
bool stores that yes/no. You can print it later or feed it to an if. The chemistry is in the comparison ph < 7.0, not in the type.
acidic ⇔ pH < 7
pH 7 is exactly neutral only at 25 °C. Kw changes with temperature, so the neutral point drifts a little.
Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
double ph = 4.2;
bool acidic = ph < 7.0;
printf("%s\n", acidic ? "acidic" : "not acidic");
return 0;
}Physics
Ice or liquid water
At standard pressure, ice melts at 0 °C. A freezer at −3 °C keeps water solid. The boolean is the whole scientific question for this check: is the sample above that point?
Phase diagrams are more interesting at other pressures. For a beaker on a bench, 0 °C is the line.
liquid if t > 0 °C (at 1 atm)
Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
double celsius = -3.0;
bool liquid_water = celsius > 0.0;
printf("liquid? %s\n", liquid_water ? "yes" : "no — ice");
return 0;
}