In the C11 standard document, the operation resu lt s of the relational operators <, >, < =, > = are specified. When true, it returns 1, when false, it returns 0, and the return type is integer.
Operator = == Similar to relational operators, it returns 1 or 0 except that the operation priority is low.
The definition of true is non-zero, so the definition of false is the 0 value of integer.
C language itself has only one_ Bool definition is a keyword.
_ Bool type is an object that stores two values of 0 and 1. It is an unsigned integer.
As shown in the following procedure_ Bool has only 0 and 1, that is, false and true. When assigning values, non-0 is regarded as 1.
Any scalar value to_ Bool type variable is assigned. If it is equal to 0, it is assigned as 0, otherwise it is assigned as 1.
#include <stdio.h> int main() { _Bool varA; varA = 2; printf("varA:%d.\n",varA); varA = -1; printf("varA:%d.\n",varA); varA = 0; printf("varA:%d.\n",varA); printf("Hello world!\n"); return 0; } $ gcc -o tof tof.c $ ./tof varA:1. varA:1. varA:0. Hello world!
In order to make it easier for programmers to use Boolean types, the standard library of C language, header file < stdpool. H >, defines types related to Boolean operations.
stdbool.h
/* Copyright (C) 1998, 1999, 2000, 2009 Free Software Foundation, Inc. This file is part of GCC. GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Under Section 7 of GPL version 3, you are granted additional permissions described in the GCC Runtime Library Exception, version 3.1, as published by the Free Software Foundation. You should have received a copy of the GNU General Public License and a copy of the GCC Runtime Library Exception along with this program; see the files COPYING3 and COPYING.RUNTIME respectively. If not, see <Licenses- GNU Project - Free Software Foundation>. */ /* * 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 are present. */ #define __bool_true_false_are_defined 1 #endif /* stdbool.h */
The header file in C, stdpool. H, defines the bool type, which is actually_ Bool.
It defines true as 1 and false as 0, which is convenient to use.
These macros are expanded into types as defined above_ Bool and constants 1 and 0.
C program using stdpool. H:
#include <stdio.h> #include <stdbool.h> int main() { bool varA; varA = 2; printf("varA:%d.\n",varA); varA = -1; printf("varA:%d.\n",varA); varA = 0; printf("varA:%d.\n",varA); varA = true; printf("varA:%d.\n",varA); varA = false; printf("varA:%d.\n",varA); printf("Hello world!\n"); return 0; } $ gcc -o tof tof.c $ ./tof varA:1. varA:1. varA:0. varA:1. varA:0. Hello world!
At the same time, we see that stdpool. H also uses__ cplusplus, the macro switch of C + + compiler, uses the following macro definition when compiling C programs with C + + compiler.
At this time, four are defined, bool, false, and true are intact, indicating that the C + + language itself has its own definition. And_ Bool is converted to bool, indicating that there is no in C + +_ Bool, use bool instead.
====================================
Let's take a look at the definitions of true and false in C + +:
Check the C++11 standard document. bool, true and false in C + + are keywords.
True and false are literal constants, and the variable value of bool type is true or false.
The procedure is as follows:
#include <stdio.h> int main() { bool varA; printf("false:%d,true:%d.\n", false, true); varA = 2; printf("varA:%d.\n", varA); varA = -1; printf("varA:%d.\n", varA); varA = 0; printf("varA:%d.\n", varA); printf("Hello world!\n"); return 0; } $ g++ -o tofplus tof.cpp $ ./tofplus false:0,true:1. varA:1. varA:1. varA:0. Hello world!
false is 0 and true is 1.
The value of a bool type variable can only be 0 or 1.
be careful:
1. The definitions of TRUE and FALSE in uppercase are not defined in C/C + + language and standard library, and they are added separately in the program.
2. The gcc version used in this article is gcc version 9.3.0. The sample code edited and compiled under Ubuntu virtual machine.