Is there a bool type in C language?

Source: Internet
Author: User

Problem
First, let's look back at 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:

[Cpp]
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:


[Cpp]
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:


[Cpp]
# 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
# Define false
# Define true
 
# 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:
[Cpp]
Bool bb = 10;
Bool aa = 0;
Printf ("% 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.