CLOSE

What is stdbool.h?

Before the introduction of the C99 standard, C did not have a built-in boolean type. Programmers often used integers (int) to represent boolean values, with 0 representing false and any non-zero value representing true. This approach, while functional, lacked clarity and type safety. The stdbool.h header was introduced in the C99 standard to address this issue by providing a dedicated boolean type and values.

Contents of stdbool.h

The stdbool.h header defines the following:

  1. Boolean Type Definition:
    • bool: This is defined as an alias for the built-in _Bool type introduced in C99.
      • The _Bool is a keyword introduced in the C99 standard to provide a boolean data type in C.
      • Key Features of _Bool:
        • Size: The size of _Bool is implementation-defined, but it is typically 1 byte (8 bits) on most modern systems. This size is sufficient to store the values 0 (false) and 1 (true).
        • Automatic Conversion: When you assign a non-zero value to a _Bool variable, it is automatically converted to 1. This ensures that _Bool variables only hold the values 0 or 1.
  2. Boolean Constants:
    • true: Defined as 1, representing the boolean true value.
    • false: Defined as 0, representing the boolean false value.
  3. Macro to Indicate Boolean Definition:
    • __bool_true_false_are_defined: Defined as 1 to indicate that the boolean type and values are defined.

stdbool.h Header File:

#ifndef __STD_BOOL_H__
#define __STD_BOOL_H__


// If it is compiled in C++ don't define bool, true, and false.
#ifndef __cplusplus
	#define bool _Bool
	#define true 1
	#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension only. */
	#define _Bool bool
	#define bool  bool
	#define false false
	#define true  true
#endif

#define __bool_true_false_are_defined 1

#endif /* __STD_BOOL_H__ */

Explanation:

1 Avoid Redefinition in C++:
#ifndef __cplusplus
	#define bool _Bool
	#define true 1
	#define false 0
  • #ifndef __cplusplus: This directive checks if the code is being compiled as C++ code. If not, it defines bool, true, and false.
  • #define bool _Bool: Defines bool as _Bool, a built-in type in C99 that represents boolean values.
  • #define true 1: Defines true as 1.
  • #define false 0: Defines false as 0.

C++ already has bool, true, and false as built-in keywords, so these definitions are skipped to avoid conflicts.

2 GNU Extension for C++:
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
	#define _Bool bool
	#define bool  bool
	#define false false
	#define true  true
#endif
  • #elif defined(__GNUC__) && !defined(__STRICT_ANSI__): Checks if the GNU C compiler (GCC) is being used and that strict ANSI compliance is not enforced.
    • __GNUC__: This macro is defined by GCC.
    • __STRICT_ANSI__: This macro is defined when strict ANSI compliance is enforced, disabling GNU extensions.

This block ensures compatibility with GNU extensions, defining _Bool, bool, true, and false as themselves.

3 Macro Indicating Boolean Type Definitions:
#define __bool_true_false_are_defined 1
  • #define __bool_true_false_are_defined 1: This macro is defined to indicate that the boolean type and constants (bool, true, and false) are available.

How to Use stdbool.h:

To use the boolean type and constants in your C code, simply include the stdbool.h header:

#include <stdbool.h>

int main() {
    bool isValid = true;  // Using the bool type and true constant
    if (isValid) {
        // Do something when isValid is true
    } else {
        // Do something else when isValid is false
    }
    return 0;
}