C and C + + Boolean type __c++

Source: Internet
Author: User

The Boolean type in C,c++ uses 1. Its own definition of "imitation boolean"

Before the C99 standard is supported, we often imitate the definition of Boolean, there are many ways, the following two kinds of common: [CPP] view plain copy print?    /* The first method */#define TRUE 1 #define FALSE 0/* The second method */enum Bool{false, true}; 2. Use of _bool

With the C99 standard support, you can use _bool to define a Boolean variable. Here is an example program.[CPP]  View plain  copy  print? #include  <stdio.h>    #include  <stdlib.h>         Int main () {       _Bool a = 1;        _Bool b = 2;    /*  use non 0 value, b value is 1 */        _Bool c = 0;       _bool d =  -1;   /*  use non 0 value, D value is 1 */               printf ("a==%d,  /n",  a);       printf ("b== %d,  /n ",  b);       printf (" c==%d,  /n ",  c);        printf ("d==%d,  /n",  d);           printf ("sizeof (_bool)  == %d  /n",  sizeof (_boOL));              system ("pause");       return EXIT_SUCCESS;  }  

Run Results: (only 0 and 12 values) [CPP]   View plain  copy  print? a==1,    b==1,   c==0,   d==1,   sizeof (_bool)  == 1   3. Use the Stdbool.h

        in C + +, the Boolean variable is defined by bool , through true and false to assign values to the Boolean variable. C99 to enable us to write code that is compatible with C + +, add a header file, stdbool.h >. In GCC, the source code for this header file is as follows: [CPP]   View plain  copy  print? /* copyright  (C)  1998, 1999, 2000 free software foundation, inc.           This file is part of GCC.   */      #ifndef  _STDBOOL_H   #define  _STDBOOL_H       #ifndef  __cplusplus      #define  bool    _Bool    #define  true    1   #define  false   0      #else  /* __cplusplus , used in C + + where you don't have to deal with it */  

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.