Home  >  Article  >  Backend Development  >  Detailed explanation of the usage of C++ Boolean types and references

Detailed explanation of the usage of C++ Boolean types and references

php是最好的语言
php是最好的语言Original
2018-08-06 09:17:214024browse

1. Boolean type

The Boolean type in C

  • C adds bool# on top of the basic type system of C language.

  • ##The only possible values ​​for

    bool in C are true and false

  • Theoretically

    bool occupies one byte

Note:

true represents the true value, The compiler internally uses 1 to represent

false represents a non-true value, and the compiler internally uses 0 to represent

In C language:

is represented by an integer Type value replaces the

bool type, commonly used 0:flase, 1:true

##C has made type enhancements and added a very rigorous
bool

type, true and false exist as keywords. In the Boolean type of C, the

bool

type only has two values: true and false. The C compiler will convert non-0 values is true, a value of 0 is converted to false. <pre class="brush:css;toolbar:false;">bool b = 0; printf(&quot;b = %d\n&quot;, b); b++; printf(&quot;b = %d\n&quot;, b); b = b - 3; printf(&quot;b = %d\n&quot;, b); // bool类型是否支持数学运算?</pre>In fact, in C language, the internal implementation of the Boolean type is implemented as a byte integer. The

bool
type supports mathematical operations, and the compiler will make adjustments internally. Non-0 is true, 0 is falseCode test:
#include <stdio.h>

int main(int argc, char *argv[])
{
    bool b = false;
    int a = b;
    
    printf("sizeof(b) = %d\n", sizeof(b));
    // sizeof(b) = 1, bool类型占一个字节
    printf("b = %d, a = %d\n", b, a);    0
    // b = 0, a = 0
    
    b = 3;    // b = 1
    a = b;  // a = 1
    
    printf("b = %d, a = %d\n", b, a);
    
    b = -5;    // b = 1
    a = b;  // a = 1
    
    printf("b = %d, a = %d\n", b, a);
    
    a = 10; // a = 10
    b = a;  // b = 1
    
    printf("a = %d, b = %d\n", a, b);
    
    a = 0;    // a = 0
    b = a;  // b = 0
    
    printf("a = %d, b = %d\n", a, b);
    
    return 0;
}

Boolean type is the basic data type in C

    can be defined Global variables of type
  • bool

  • can be defined# Constants of type
  • bool

  • can be defined# A pointer of type ##bool
  • can define an array of type
  • bool
  • ......

  • 2. Ternary operator
The ternary operator has been upgraded in C

Consider whether the following code is correct, respectively in C language and C Compile and run the test in the environment

int a = 1;
int b =2;
(a < b ? a : b) = 3;
printf("a = %d, b = %d\n", a, b);
// 在C语言中报错
// 在C++中,结果a = 3

Ternary operator

The ternary operator in C language returns the variable value
  • cannot be used as an lvalue
    • #The ternary operator in C can directly return the variable itself
  • Can be used as both an rvalue and an lvalue
    • Note:
Possible values ​​returned by the ternary operator If one of them is a constant value, it cannot be used as an lvalue.

The ternary operator can only be used as an lvalue when all possible returns are variables. A constant and a variable cannot be used as an lvalue. What is the significance of such an upgrade to the ternary operator using

C?

When all possible returns of the ternary operator are variables,

returns the variable itself

, which leads to a new concept: Quote

3, Quote

3.1 Variable name

A variable is an alias for an actual continuous storage space. In the program, the storage space is applied for and named through variables. The storage space can be used through the name of the variable.
Question: Can a continuous storage space have only one alias?


3.2 ReferenceDetailed explanation of the usage of C++ Boolean types and references

Reference in C

The concept of reference is added in C
int a = 4;
int& b = a;     // b作为a的别名
b = 5;        // 操作b就是操作a
  • The reference can be regarded as an alias of a defined variable
    • The syntax of the reference:
    • Type& name = var;
    • Note:
    Ordinary references must be initialized with variables of the same type when they are defined.

    What does C do with the ternary operator?
      int a = 1;
      int b = 2;
      (a 
    • When the possible returns of the ternary operator are all variables, what is returned is
        variable reference
      • When there is a constant in the possible return of the ternary operator, the
      • value is returned
      4. Summary# The
      ##bool

      type is a newly added basic type of C. The values ​​of the

      bool type can only be true

      and falseThe ternary operator in C can be used as an lvalueReferences in C can be used as aliases of variables

      The possible returns of the ternary operator are all variables When, the reference is returned

      Related articles:

      php boolean (Boolean) type usage example

      PHP Boolean Type data type false true usage introduction

      The above is the detailed content of Detailed explanation of the usage of C++ Boolean types and references. For more information, please follow other related articles on the PHP Chinese website!

      Statement:
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn