On boolean bool data type in C language programming _c language

Source: Internet
Author: User

We know that in C + + there is a special type of bool used to represent true or false. But there is no such type in C (at least I always think), the value of the expression is 0 false, not 0 is true. So the conditional Judgment statement (if (...), while (...)) Very flexible, even a pointer type can be a conditional expression.
To make the program clearer, we often give the following macro definitions:

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

This is the most common form of writing that can be recognized by any C language compiler.
Today, I saw such a line in a program #include, this unfamiliar header file with an STD and also related to bool, causing my vigilance, immediately Baidu. Know this is the C99 standard new header file, intended to introduce bool type, make it compatible with C + +. Then I looked at the source code of the header file, which reads as follows:

/
* 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
#define FALSE
#define TRUE #endif/
* __cplusplus */////* Signal that all the
definitions are present. */
#define __BOOL_TRUE_FALSE_ARE_DEFINED 1
    #endif/* stdbool.h * *

This header is very short and very clear, but I have an interest in _bool this type. Another turn Baidu, found that this is the C99 standard introduced a new keyword. Yes, it's a keyword, not a macro definition, and it's not a typedef. by sizeof (_bool), you know that this type is 1 bytes, and whether you assign any non-0 integer value to this type of variable, it is 1, which means that he is not an alias for other integer types.
Well, in fact, the C language is not without Boolean type, can only say before the C99 standard did not. Now, as long as you include the stdbool.h header file in your source file, you can use the bool type in C as C + +.
So if it's C99 before the standard, we need to define bool ourselves, like

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

So let's take a look at the relevant definitions in the C language:
the difference between 1.false/true and false/true:
false/true is the new keyword in the standard C + + language, and false/true is through #define, which is used
Is the difference between the solution in C and C + +, the following 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, and false/true is of type bool; So the two are different, but
We don't have that feeling in use because C + + will help you do implicit conversions.
The size of the 2.bool differs from BOOL:
bool occupies 1 bytes in C + +, and bool is the int type, and the size of int is dependent on the specific environment;
: False/true occupies only 1 bytes, and true/false depending on the circumstances, the following is bool in Windef
Definition in. h: typedef int BOOL;
the difference between 3.NULL and 0:
Let's take a look at the definition of NULL in windef.h:

#ifndef  null 
#ifdef  __cplusplus//This is to indicate that the program is compiled with C + + 
#define  NULL  0 
#else 
#define  NULL  ((void  *) 0) 
#endif 
#endif 

So there is no difference between them, but in C there is a forced type conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.