Skip to content

Booleans in C

An introduction to how to use booleans in C

C originally did not have native support for boolean values.

C99, the version of C released in 1999/2000, introduced a boolean type.

To use it, however, you need to import a header file, so I’m not sure we can technically call it “native”. Anyway, we do have a bool type.

You can use it like this:

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

int main(void) {
  bool isDone = true;
  if (isDone) {
    printf("done\n");
  }

  isDone = false;
  if (!isDone) {
    printf("not done\n");
  }
}

If you’re programming the Arduino, you can use bool without including stdbool because bool is a valid and built-in C++ data type, and the Arduino Language is C++.

In plain C, remember to #include <stdbool.h> otherwise you’ll get a bunch of errors at declaration and any time you use the bool variable:

➜  ~ gcc hello.c -o hello; ./hello
hello.c:4:3: error: use of undeclared identifier
      'bool'
  bool isDone = true;
  ^
hello.c:5:7: error: use of undeclared identifier
      'isDone'
  if (isDone) {
      ^
hello.c:8:8: error: use of undeclared identifier
      'isDone'
  if (!isDone) {
       ^
3 errors generated.

→ Get my C Handbook

download all my books for free

  • javascript handbook
  • typescript handbook
  • css handbook
  • node.js handbook
  • astro handbook
  • html handbook
  • next.js pages router handbook
  • alpine.js handbook
  • htmx handbook
  • react handbook
  • sql handbook
  • git cheat sheet
  • laravel handbook
  • express handbook
  • swift handbook
  • go handbook
  • php handbook
  • python handbook
  • cli handbook
  • c handbook

subscribe to my newsletter to get them

Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.

Related posts about clang: