Together talk C Chestnut Bar (168th: C language Instance--c Boolean--bool type in language)

Source: Internet
Author: User
Tags true true

Ladies and gentlemen, crossing, the last time we talked about the control of the terminal character Color example, this time we say the example is:C language of the Boolean –bool type . Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, in the digital circuit we often encounter Boolean type, in fact, in the process of writing software will also use Boolean type. There are only two values for a variable of type Boolean: True and False. in the actual programming process, we often define macros, then use 1 for true, and 0 for false. Here's a concrete example:

#define TRUE    1  #define FALSE   0

The example defines two macros, one is true, its value is 1, it is true, the other is false, and its value is 0, which means false. Such code often appears in the program. In fact, in the example we have in the previous chapter, there are often such code, it may be thought that this code has become accustomed to.

Crossing says that although there are only two lines of code, it is cumbersome to define such a macro every time. This crossing is right, in fact, not only you have such trouble, many people are also very upset about it. The C language then treats the Boolean type as its own inline type, meaning that you can use the Boolean type as you would with an inline type such as Int,char. It is used in the following ways:

booltrue;

You need to include a header file when you use the bool type: #include<stdbool.h> There is one more way to use it:

bool _Bool_value = 1;

You can use the _bool type directly in this method without having to include any header files. However, we do not recommend this method of use. There is hardly any type in C that has an underscore in front of it and how discordant it looks.

Next, we'll show you how to use Boolean types with specific examples.

#include <stdio.h>#include <stdbool.h>intMain () {//_bool flag = 1;     BOOLFlag =true;intindex =3; while(index--> Flag) {if(flag) {printf("flag is:%d \ n", flag); Flag =false; }Else{printf("flag is:%d \ n", flag); Flag =true; }    }return 0;}

In the example above, we have defined the Boolean type variable flag and assigned it a value of true. Then output its truth and false values in decimal form , the following is the result of the program operation:

is1   //布尔类型的真值is0   //布尔类型的假值

As you can see from the running results above, the values of the Boolean type can be converted to decimal 1 and 0. In addition, we also compare the size of variables of boolean type and int type in the example, which indicates that a Boolean type variable can be converted to an int type variable. When used, however, we recommend that you use True and False to assign a variable of the Boolean type instead of converting the value of the INT type variable to the value of the Boolean type variable.

At this point, crossing actually said that the above example has a compile error. I want to ask this crossing, do you use GCC? If not, use GCC to compile the above example, or to upgrade your compiler. Because the Boolean type is the newly added type in C99, there is no Boolean type in C89 and in the previous C language. Because some older compilers or compilers that do not support C99 do not know the Boolean type, there is a compilation error.

I am using gcc (version 4.8), this version of GCC supports Boolean type, and there is a Boolean type definition, we can hit the opening file :stdbool.h to see:

less /usr/lib/gcc/i686-linux-gnu/4.8/include

After running the command in the terminal, you can open the file, the following is the contents of the file, please refer to:

/ * * ISO C standard:7.16 Boolean type and Values <stdbool.h> * *#ifndef _stdbool_h#define _STDBOOL_H#ifndef __cplusplus#define BOOL _bool#define TRUE 1#define FALSE 0#else/* __cplusplus * // * Supporting <stdbool.h> in C + + is a GCC extension. */#define _bool Bool#define BOOL BOOL#define FALSE False#define True True#endif/* __cplusplus * //* Signal that all the definitions is present. */#define __BOOL_TRUE_FALSE_ARE_DEFINED 1#endif/* stdbool.h * /

You crossing, for example of the type of Boolean –bool in C, let's talk about it here. I want to know what the following example, and listen to tell.

Together talk C Chestnut Bar (168th: C language Instance--c Boolean--bool type in language)

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.