Boolean in C

An introduction to how to use boolean values in C

C originally did not have native support for boolean values.

C99, versionCposted on1999⁄2000, The Boolean type is introduced.

However, to use it, you need to import a header file, so I'm not sure we can technically call it "native". Anyway, we do have aboolTypes of.

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 are programming the Arduino, you can useboolNot includedstdboolbecauseboolIs a valid built-in C++ data type, andArduino languageIt is C++.

In pure C language, please remember#include <stdbool.h>Otherwise, at the time of declaration and every useboolChanging:

➜  ~ 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.  

Download my free C Handbook


More clang tutorials: