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 */