Is there a bool type in C language?

Source: Internet
Author: User
First of all, I would like to recall whether the bool (Boolean) type has been used in your C program? The bool type is generally used to indicate true or false. It is a basic data type in programming. However, the C language does not directly support the bool type. Some beginners who are new to C, or who are not very proficient in C, may find it strange. Why is the bool type used in C ++, Java, and other languages unavailable in C?
Problem Analysis

A boolean variable. If it is 0, it indicates false. If it is not 0, it indicates true. However, the bool type is not supported in the original C language. You can use the int type to define the bool type. For example:

typedef int bool;#define TRUE 1#define FALSE 0

 

Then you can use the bool type in the program. Using a 32-bit int type for bool is a waste of memory resources, so char can be used in memory-sensitive programs to define the bool type:

typedef char bool;#define TRUE 1#define FALSE 0

Further discussion

The C language is not of the bool type, but not before the c99 standard. The header file added in the c99 standard introduces the bool type, which is compatible with the bool type in C ++. The header file is stdbool. H, and its source code is as follows:

#ifndef _STDBOOL_H#define _STDBOOL_H#ifndef __cplusplus#define bool_Bool#define true1#define false0#else /* __cplusplus *//* Supporting <stdbool.h> in C++ is a GCC extension.  */#define _Boolbool#define boolbool#define falsefalse#define truetrue#endif /* __cplusplus *//* Signal that all the definitions are present.  */#define __bool_true_false_are_defined 1#endif /* stdbool.h */

 

In the code, _ bool is a New Keyword introduced by the c99 standard for the bool type. The value of sizeof (_ bool) is 1, and it is the bool type on the surface. Since it is of the bool type, 0 indicates false, and any other value indicates true. We can perform a small experiment to test it:

bool bb=10;bool aa=0;printf(" %d %d  \n",bb,aa);

The output result is 1 and 0, which indicates that when the value of the bool type variable is not 0, only the value is 1.


Finally, when you need to use the bool type, we recommend that you use the c99 standard bool type instead of defining it yourself. Just introduce the header file <stdbool. h>. The header file is located in

/Usr/lib/GCC/i386-redhat-linux/4.1.1/include/stdbool. H (take my own Linux system as an example ).

Related Article

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.