Boolean Type in C Language

Source: Internet
Author: User

Boolean Type in C Language
Boolean Type in C language 1. Basic Knowledge

First, bool true false is the keyword in C ++. By default, these characters are not supported in C!

Ii. Details

No Boolean-related content defined in the C89 (ansi c) Standard

 

However, a New Keyword _ Bool is defined in the C99 standard, and a new header file <stdbool. h> standardizes Boolean operations to facilitate calls by programmers!

 

The following content is defined in the <stdbool. h> header file:

 

# Define bool_Bool

# Define false0

# Define true1

 

In this way, the compiler that supports the C99 standard only needs to include stdbool. h, so it is very convenient to use the commonly used bool false true to operate the boolean type.

 

However, some compilers may not support the C99 standard! That is, you cannot use the keyword _ Bool and may not be able to contain the stdbool. h header file. Therefore, you can define some Boolean operations:

 

# Define bool int

# Define true 1

# Define false 0

Iii. analysis summary

That is, the _ Bool and stdbool header files cannot be used in compilers that do not support the C99 standard. h. You can use _ Bool unconditionally in a compiler that supports the C99 standard. Because it is a keyword, its value can only be 0 or 1. If you want to use bool, false, true indicates that the header file stdbool must be included. h

 

Iv. instance test code

Use the following program to test in VS2015:

# Include <stdio. h>

Int main (void)

{

_ Bool a =-112;

Printf ("% d \ n", );

Getchar ();

Return 0;

}

Program output 1

That is, if 0 is assigned to a, 0 is output. If a value of _ Bool type a is assigned any other value, it will be converted to 1, that is, if it is not 0, it will be changed to 1!

 

The following programs are compiled in VS2015, indicating that VS2015 supports the C99 standard.

# Include <stdio. h>

# Include <stdbool. h>

Int main (void)

{

_ Bool a = 1;

Bool B = true;

Bool c = false;

If (a = true)

{

Printf ("% d \ n", );

Printf ("% d \ n", B );

Printf ("% d \ n", c );

}

Getchar ();

Return 0;

}

 


 

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.