Home > Back-end >  About the Boolean type in the c language
About the Boolean type in the c language

Time:04-30

Can you explain it in detail?

CodePudding user response:

Enumeration defined, FALSE=0, TRUE=1;

Enum is one of the key words, like struct, union

CodePudding user response:

The
reference 1/f, confident boy reply:
enumeration defined, FALSE=0, TRUE=1;

Enum is a key word, similar to a struct, union

In Boolean form c language exactly what role?

CodePudding user response:

refer to the second floor for dolphin response:
Quote: reference 1/f, confident boy reply:
enumeration defined, FALSE=0, TRUE=1;

Enum is a key word, similar to a struct, union

In Boolean form c language exactly what role?

It is an integer value, true or false;


CodePudding user response:

Before C99, strictly speaking, there is no Boolean type C language, but this does not prevent the C language, and its interface, and other language after all in the programming language practice is 0 for false and non-zero is true, the problem is that many people think it is a language and completeness of flaws and defects, like someone's house boiled milk have special milk pan, you home cooking and boiled milk all use the same wok,

In WG14, many members have the background of the C + +, the dao son liked to add in the C standard C + + stuff, but most will not adopted by standard committee, because many are bootleg inside, which is the manufacturer's representative all hope that their company's extension features added to the standard,

May be input too many people, WG14 compromise finally, decided to put the Boolean type to join the next C99 standard C language, ideally, it need to introduce a Boolean keyword, or at least a bool, easy to remember, however, before this, there are a lot of perfectionism programmers for the sake of convenience, cleanliness, satisfy their language has been defined in the program's own "bool", and there must be thousands of such of the program, in order to be able to compile these normal an existing program and C99 decided to introduce a _Bool underlined the key word, so it is difficult to have the conflict,

Keyword _Bool is used as a type specifier, used to declare a variable, or function parameters and return types, this type of variable has the following characteristics:

1, it is an unsigned integer types;
2, 0 s and 1 s can save at least two values;
3, converts any scalar value _Bool type, zero value is converted to a zero, a non-zero value is converted to 1;

C language rules, in addition to a domain (bit - fields), and any object (variable) is composed of one or more consecutive bytes, as a result, the length of the type _Bool is at least one byte,

For convenience, in the C language standard library part adds a head & lt; Stdbool. H> The head is very simple, defines several macros:

 # define bool _Bool 

# define true 1
# define false 0

# define __bool_true_false_are_defined 1


Finally a macro __bool_true_false_are_defined is a mark, used to indicate the previous three macros have been defined, you can use,
  • Related