Analysis of the example of Cracket + practical True and false
This article mainly introduces the example analysis of Chammer Craft + practical True and false, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand.
In the C11 standard document, the operation result of the relational operator and = is specified, which returns 1 when it is true, 0 if it is false, and the return type is integer.
The operators = =,! =, and relational operators are similar to relational operators, returning 1 or 0 except for a lower priority.
The definition of True is non-zero, so the definition of False is the 0 value of the integer.
The C language itself has only one _ Bool definition, which is a keyword.
The _ Bool type is an object that stores values 0 and 1, and is an unsigned integer.
As shown in the following program, _ Bool has only 0 and 1, that is, false and true, and both non-zero values are regarded as 1 when assigned.
Any scalar value assigns a value to a variable of type _ Bool. If it is equal to 0, the assignment is 0, otherwise it is assigned to 1.
# include 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$. / tofvarA: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 files, defines the 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 modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.Under Section 7 of GPL version3, you are granted additionalpermissions described in the GCC Runtime Library Exception, version3.1, as published by the Free Software Foundation.You should have received a copy of theGNU General Public License anda copy of the GCC Runtime Library Exception along with this program;see the files COPYING3 and COPYING.RUNTIME respectively. If not, see. * / / * * ISO C Standard: 7.16 Boolean type and values * / # ifndef _ STDBOOL_H#define _ STDBOOL_H#ifndef _ _ cplusplus#define bool _ Bool#define true 1#define false 0#else / * _ _ cplusplus * / / * Supporting 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, stdbool.h, defines the bool type, which is actually _ Bool.
True is defined as 1, and false is 0, which is convenient to use.
These macros are expanded into types _ Bool and constants 1 and 0 as defined above.
Using the C program of stdbool.h:
# include # include 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$. / tofvarA:1.varA:1.varA:0.varA:1.varA:0.Hello world!
At the same time, we see that the _ _ cplusplus macro switch of the C++ compiler is also used in stdbool.h. If you use the C++ compiler to compile a C program, use the following macro definition.
At this time, four are defined, bool, false, and true are all intact, indicating that the C++ language has its own definitions. The conversion of _ Bool to bool indicates that there is no _ Bool in C++ and uses bool instead.
Let's take a look at the definitions of true and false in C++:
Take a look at the Clear11 standard documents. Bool, true and false are all keywords in C++.
True and false are literal constants, and variables of type bool are true or false.
The following procedure shows:
# include 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 } $tofplus tof.cpp$ +-o tofplus tof.cpp$. / tofplusfalse:0,true:1.varA:1.varA:1.varA:0.Hello world!
False is 01.True is 1.
The value of a variable of type bool can only be 0 or 1.
Note:
1. The definitions of uppercase TRUE and FALSE are not defined in the Cmax Clipper + language and the standard library, and are added separately in the program.
2. The gcc version used in this article: gcc version 9.3.0 compiled sample code under the Ubuntu virtual machine.
Thank you for reading this article carefully. I hope the article "the sample Analysis of Cpicket + practical True and false" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!