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.

ExpressionTrue when
a == ba equals b
a != ba is not equal to b
a < ba is less than b
a <= ba is less than or equal to b
a > ba is greater than b
a >= ba 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> gives bool, true, and false.
  • Comparisons such as == and < produce 0 or 1.
  • printf with %d prints 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;
}

FAQ: C Booleans

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c boolean examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c boolean in this C C lesson (C Booleans).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.