C Programming/stdbool.h

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The header stdbool.h in the C Standard Library for the C programming language contains four macros for a Boolean data type. This header was introduced in C99.

The macros as defined in the ISO C standard are :

  • bool which expands to _Bool
  • true which expands to 1
  • false which expands to 0
  • __bool_true_false_are_defined which expands to 1

which can be used in an example

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

int main(void) {
    bool keep_going = true;  // Could also be `bool keep_going = 1;`
    while(keep_going) {
        printf("This will run as long as keep_going is true.\n");
        keep_going = false;    // Could also be `keep_going = 0;`
    }
    printf("Stopping!\n");
    return EXIT_SUCCESS;
}

which will output

This will run as long as keep_going is true.
Stopping!

External links[edit | edit source]

  • stdbool.h: boolean type and values – Base Definitions Reference, The Single UNIX® Specification, Issue 7 from The Open Group