Knowledge Point 1. Are there bool types in C language?
From http://blog.csdn.net/liuqiqi677/article/details/6703615
I have never noticed that when I recently wrote a DSP Algorithm in C language, I accidentally found that the bool type variables defined in my function are in vc6.0 (I mainly use it to check for syntax errors) in the compilation, an error is reported, indicating that the bool type is not defined. After using C and C ++ for so long, I have always thought that the basic type of bool should be the built-in type of C language. How can it be not defined? It turns out that my thoughts are totally wrong.
With the Internet, all problems have become so easy that I can easily find relevant answers.
There is no bool (Boolean) type in C language, and it is only available in C ++. That is to say, it is no problem to use the bool type in C ++. The bool type has only two values: true = 1 and false = 0.
However, bool type variables are defined in the c99 standard. In this case, as long as the header file <stdbool. h> is introduced, the bool type can be used normally in the C language. I have verified this in fedora9. Its built-in compiler is GCC 4.3.0, which complies with the c99 standard. However, in vc6.0, the error "stdbool. H" is not found. It seems that vc6.0 is old.
Therefore, to be compatible with various compilers, we recommend that you define them by yourself. Pai_^
# Define bool char
# Define ture 1
# Define false 0
The c99 standard defines a new keyword _ bool, which provides the boolean type. In the past, C programmers always used their own methods to define the boolean type.
0 indicates false, and non-0 indicates true.
The char type may be used to represent a boolean type, or the int type may be used to represent a boolean type.
Many function libraries define their own Boolean types and corresponding macros, enumerations, and typedef.
Now c99 brings the native Boolean Type of C language.
# Include<Stdbool. h>
This header file defines macros such as bool, true, and false.
# Ifndef_ Cplusplus
If it is not c ++, it is pure C, then the macro bool is equivalent to the _ bool type.
# DefineBool_ Bool
# DefineTrue 1
# DefineFalse 0
# Else/* _ Cplusplus */
If it is C ++, The _ bool type is defined as bool.
/* Supporting <stdbool. h> in C ++ is a GCC extension .*/
# Define_ BoolBool
# DefineBool
# DefineFalse false
# DefineTrue
# Endif/* _ Cplusplus */
/* Signal that all the definitions are present .*/
# DEFINE _ bool_true_false_are_defined 1
Check whether the macro above is = 1. If yes, the above definition is available.
The c2008 draft states:
Anobject declared as type _ boolis large enough to store the values 0 and1.
That is to say, it only specifies that the _ bool type can store at least the values 0 and 1. No specific size is specified. This gives the compiler free play.
I tested it 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.
Let's see how the _ bool type is assigned:
Boolboolean =-11212112;
Printf("Boolean: % d \ n", Boolean );
-- Boolean: 1
It seems that the GCC compiler has its own Conversion Processing for the _ bool type. If 0 is assigned to the _ bool type, 0 is assigned. If it is any other data, it will be assigned a value of 1! (I just don't know if other compilers are doing this. The c2008 standard draft didn't elaborate on this part)
I used it all the time! Logical non-OPERATOR:
If (! Flag ){
}
To indicate 0 and non-0. Now the _ bool type has only the values 0 and 1!
Knowledge Point 2. bool type in objective-C
1. the bool type in objective-C is actually a signed Char definition (typedef), which uses an 8-bit storage space. Yes is defined as 1, and no is defined as 0.
Objective-C does not treat the bool type as a true boolean type that can only save yes or no. The compiler recognizes bool as an 8-bit binary number. Yes or no is just a convention.
If you accidentally assign an integer value longer than 1 byte (such as the short and INT values) to a bool variable, only the low byte will be used as the bool value. Suppose that the low byte is exactly 0 (for example, 8960, written in hexadecimal format as 0x2300 ),
The bool value is 0, that is, the NO value.
The difference between false/true and false/true: 1. the difference between false/true and false/true: false/true is a new keyword in the Standard C ++ language, while false/true is through # define, the purpose is to solve the difference between the C and C ++ environments. The following is false/true in windef. h definition: # ifndef false # define false 0 # endif # ifndef true # define true 1 # endif that is false/true is the int type, and false/true is the bool type; so the two are different, but we don't have this feeling in use, because C ++ will help you do implicit conversion. 2. the difference between bool size and bool: bool occupies 1 byte in C ++, while bool is int type. The size of int type depends on the specific environment: false/true only occupies 1 byte, while True/false indicates that the following bool is in windef. definition in H: typedef int bool; 3. difference between null and 0: Let's take a look at windef. the definition of null in H: # ifndef null # ifdef _ cplusplus // This indicates that the program is compiled using C ++ # define null 0 # else # define null (void *) 0) # endif: there is no difference between them, but a forced type conversion will be performed in C.
Knowledge Point 3. Differences between bool and bool
I,
1. Different Types
Bool is int type
Bool is Boolean
2. Different Lengths
Bool has only one byte
The bool length depends on the actual environment. Generally, it can be regarded as 4 bytes.
3. Different values
Bool values: false and true, which are the difference between 0 and 1.
Bool values: false and true, which are the difference between 0 and non-0.
II:
Bool is a standard C ++ data type, which can be set to true or false. Occupies one byte separately,
If multiple bool objects are listed together, each bit may be occupied, depending on the compiler.
Bool is the typedef int bool defined by Microsoft. Unlike bool, It is a three-value logic,
True/false/error. If the returned value is greater than 0, the integer is true, the value 0 is false, and the value-1 is error.
In Win32 API, many functions with return values of bool are three-value logic. For example, getmessage ().
III:
Differences between large and small bool:
1. Different Types
Bool is int type
Bool is Boolean
2. Different Lengths
Bool has only one byte
The bool length depends on the actual environment. Generally, it can be regarded as 4 bytes.
3. Different values
Bool values: false and true, which are the difference between 0 and 1.
Bool values: false and true, which are the difference between 0 and non-0.
4. Example
Bool x = 3; // alarm
Bool x = 1; // correct
Bool x = 3; // correct
Bool x = 3.3; // alert
Note: Windows defines basic variables for compatibility issues.
Typedef unsigned long DWORD;
Typedef int bool;
Typedef unsigned char byte;
Typedef unsigned short word;
Typedef float;
Typedef float * pfloat;
Typedef bool near * pbool;
Typedef bool far * lpbool;
Typedef byte near * pbyte;
Typedef byte far * lpbyte;
Typedef int near * pint;
Typedef int far * lpint;
Summary: There are many true conditions for such boolean values, but false conditions generally mean false when the value is equal to 0, therefore, we can determine whether the returned value is false.
Bool and bool knowledge Sets