Discussion on Boolean bool data types in C language programming

  • 2020-05-05 11:38:36
  • OfStack

We know that there is a special bool type in C++, which means true or false. But there is no such type in C (at least, that's what I've always thought), where the value of the expression 0 is false and non-0 is true. So conditional judgment statement (if(...)) , while (...). ) is so flexible that even a pointer type can be a conditional expression.
To make the program clearer, we often give the following macro definition:


  typedef int BOOL;
  #define TRUE 1
  #define FALSE 0

This is the most common way to write it and is acceptable to any compiler of the C language.
Today, I saw this line #include in a program. This strange header file also starts with std and is related to bool, which aroused my alarm. We learned that this is a new header file for the C99 standard, which is intended to introduce the bool type and make the sum C++ compatible. Then I took a look at the source code for the header file and read:


  /*
  * ISO C Standard: 7.16 Boolean type and values
  */
  #ifndef _STDBOOL_H
  #define _STDBOOL_H
  #ifndef __cplusplus
  #define bool _Bool
  #define true 1
  #define false 0
  #else /* __cplusplus */
  /* Supporting in C++ is a GCC extension. */
  #define _Bool bool
  #define bool bool
  #define false false
  #define true true
  #endif /* __cplusplus */
  /* Signal that all the definitions are present. */
  #define __bool_true_false_are_defined 1
  #endif /* stdbool.h */

The header file is short and clear, but I'm interested in the _Bool type. Turn again baidu, discover this is the new keyword that C99 standard introduces. Yes, it's the keyword, not the macro definition, and it's not typedef. Through sizeof (_Bool); We know that this type takes 1 byte, and that any non-0 integer value assigned to a variable of this type is 1, which means that it is not an alias for another integer type.
Well, to put it bluntly, the C language is not without Boolean types, but without them before the C99 standard. Now as long as you include the stdbool.h header in the source file, you can use the bool type as C++ in the C language.
So if it is the standard before C99, we need to define bool by ourselves, such as


typedef enum {false = 0, true = 1} bool;

So let's take a look at the C definition:
1. The difference between FALSE/TRUE and false/true:  
      false/true is the new keyword in the standard C++ language, while FALSE/TRUE is #define, which USES
FALSE/TRUE is the definition of FALSE/TRUE in windef.h:


#ifndef  FALSE 
#define  FALSE  0 
#endif 
#ifndef  TRUE 
#define  TRUE  1 
#endif 

That is, FALSE/TRUE is of type int, while false/true is of type bool; So they're not the same, but
We don't have that feeling in use because C++ will do the implicit conversion for you.
2. Difference between the size of bool and BOOL:
bool takes up 1 byte in C++, while BOOL is of type int. So
For example, false/true only takes 1 byte, while TRUE/FALSE follows BOOL in windef
, depending on the context . Definition in h: typedef     int     BOOL;
3. The difference between NULL and 0:  
      again, let's look at the definition of NULL in h:


#ifndef  NULL 
#ifdef  __cplusplus// This is an indication of use C++ To compile the program  
#define  NULL  0 
#else 
#define  NULL  ((void  *)0) 
#endif 
#endif 

So there's no difference, except that C does a cast.


Related articles: