C99 's new keyword _bool__c language

Source: Internet
Author: User

The C99 standard defines a new keyword _bool that provides a Boolean type. Previously, C programmers always used their own methods to define Boolean types.

0 is false, and not 0 is true.

You might use the char type to represent a Boolean type, or you might use an int type to represent a Boolean type.

Many function libraries define their own Boolean types and corresponding macros, enumerations, and typedef.

Now C99 has brought in the native Boolean type of C language.


#include<stdbool.h>


This header file defines a macro such as Bool,true,false.

#ifndef__cplusplus

If it is not C + +, it is pure C, then defining the macro bool is equivalent to the _bool type.

#definebool_bool

#defineTrue 1

#defineFalse 0


#else/* __cplusplus * *

If it is C + +, then the _bool type is defined as bool type.

/*supporting <stdbool.h> in C + + is a GCC extension. */

#define _bool Bool

#defineBOOL

#defineFalse

#defineTrue


#endif/* __cplusplus * *


/*signal that all the definitions are present. * *

#define__bool_true_false_are_defined 1


See if the above macro is ==1, and if so, it means the above definitions are available.


The draft C2008 said:

AnObject declared as Type_boolis large enough to store the values 0 and1.

That is, only the size of the _bool type is specified to hold at least 0 and 1 of the two values. Does not specify the specific size. This is given to the compiler to play it free.


I did a test with GCC:

printf ("sizeof (BOOL):%d\n",sizeof(bool));

printf ("sizeof (char):%d\n",sizeof(char));

printf ("sizeof (int):%d\n",sizeof(int));

printf ("sizeof (long):%d\n",sizeof(long));


--sizeof (BOOL): 1

--sizeof (char): 1

--sizeof (int): 4

--sizeof (Long): 8

It seems that GCC uses char to implement the _bool type.


See what happens when you assign a value to a _bool type:

boolboolean=-11212112;

printf ("boolean:%d\n", Boolean);


--boolean:1

It appears that the GCC compiler treats the _bool type with its own conversion process. If 0 is assigned to the _bool type, then a value of 0 is assigned. If it is any other data, it will be assigned a value of 1. (Just don't know if other compilers are doing this, the C2008 standard draft does not elaborate on this part)


Used all the time. Logical non-operator:

if (!flag) {

}

To represent 0 and not 0. Now the _bool type is only 0 and 1.


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.