zoukankan      html  css  js  c++  java
  • C++解析(3):布尔类型与三目运算符

    0.目录

    1.布尔类型

    2.三目运算符

    3.小结

    1.布尔类型

    C++中的布尔类型:

    • C++在C语言的基本类型系统之上增加了bool
    • C++中的bool可取的值只有truefalse
    • 理论上bool只占用一个字节

    注意:

    • true代表真值,编译器内部用1来表示
    • false代表非真值,编译器内部用0来表示

    布尔类型的值:

    • bool类型只有true(非0)和false(0)两个值
    • C++编译器会将非0值转换为true0值转换为false

    下面的代码输出什么?

    #include <stdio.h>
    
    int main()
    {
        bool b = 0;
        printf("b = %d
    ", b);
        
        b++;
        printf("b = %d
    ", b);
        
        b = b - 3;
        printf("b = %d
    ", b);
        
        return 0;
    }
    

    以上代码gcc编译不过,g++编译成功。
    运行结果如下:

    [root@bogon Desktop]# gcc test.c
    test.c: In function ‘main’:
    test.c:5: error: ‘bool’ undeclared (first use in this function)
    test.c:5: error: (Each undeclared identifier is reported only once
    test.c:5: error: for each function it appears in.)
    test.c:5: error: expected ‘;’ before ‘b’
    test.c:6: error: ‘b’ undeclared (first use in this function)
    [root@bogon Desktop]# g++ test.c
    [root@bogon Desktop]# ./a.out 
    b = 0
    b = 1
    b = 1
    

    布尔类型是C++中的基本数据类型:

    • 可以定义bool类型的全局变量
    • 可以定义bool类型的常量
    • 可以定义bool类型的指针
    • 可以定义bool类型的数组
    • 。。。

    下面代码输出什么?

    #include <stdio.h>
    
    int main()
    {
        bool b = false;
        int a = b;
        printf("sizeof(b) = %d
    ", sizeof(b));
        printf("b = %d, a = %d
    ", b, a);
        
        b = 3;
        a = b;
        printf("b = %d, a = %d
    ", b, a);
        
        b = -5;
        a = b;
        printf("b = %d, a = %d
    ", b, a);
        
        a = 10;
        b = a;
        printf("a = %d, b = %d
    ", a, b);
        
        a = 0;
        b = a;
        printf("a = %d, b = %d
    ", a, b);
        
        return 0;
    }
    

    运行结果:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    sizeof(b) = 1
    b = 0, a = 0
    b = 1, a = 1
    b = 1, a = 1
    a = 10, b = 1
    a = 0, b = 0
    

    2.三目运算符

    C++对三目运算符进行了升级
    下面的代码正确吗?

    #include <stdio.h>
    
    int main()
    {
        int a = 1;
        int b = 2;
        
        (a < b ? a : b) = 3;
        printf("a = %d, b = %d
    ", a, b);
        
        return 0;
    }
    

    运行结果:

    [root@bogon Desktop]# gcc test.c
    test.c: In function ‘main’:
    test.c:8: error: lvalue required as left operand of assignment
    [root@bogon Desktop]# g++ test.c
    [root@bogon Desktop]# ./a.out 
    a = 3, b = 2
    

    C语言中的三目运算符返回的是变量值——不能作为左值使用;
    C++中的三目运算符可直接返回变量本身——既可作为右值使用,又可作为左值使用。

    注意:三目运算符可能返回的值中如果有一个是常量值,则不能作为左值使用。

    C++对三目运算符做了什么?

    • 当三目运算符的可能返回都是变量时返回的是变量引用
    • 当三目运算符的可能返回中有常量时返回的是值

    那么何为引用呢?

    变量名回顾:

    • 变量是一段实际连续存储空间的别名
    • 程序中通过变量来申请并命名存储空间
    • 通过变量的名字可以使用存储空间

    问题:一段连续的存储空间只能有一个别名吗?

    在C++中新增加了引用的概念:

    • 引用可以看作一个已定义变量的别名
    • 引用的语法:Type& name = var;


    注意:普通引用在定义时必须用同类型的变量进行初始化
    下面的代码会输出什么?

    #include <stdio.h>
    
    int main()
    {
        int a = 4;
        int& b = a;
        
        b = 5;
        
        printf("a = %d
    ", a);
        printf("b = %d
    ", b);
        printf("&a = %p
    ", &a);
        printf("&b = %p
    ", &b);
        
        return 0;
    }
    

    运行结果如下:

    [root@bogon Desktop]# g++ test.cpp
    [root@bogon Desktop]# ./a.out 
    a = 5
    b = 5
    &a = 0x7ffc082aaec4
    &b = 0x7ffc082aaec4
    

    (操作b就等于操作a,意味着b这个标识符和a这个标识符代表了内存里面同一段连续的空间。结论:在C++中完全可以对同一段连续的内存起任意多的别名。)

    以下三个程序C++编译器编译都不过,请自行查找错误。

    // test1.cpp
    #include <stdio.h>
    
    int main()
    {
        int a = 4;
        float& b = a;
        
        b = 5;
        
        printf("a = %d
    ", a);
        printf("b = %d
    ", b);
        printf("&a = %d
    ", &a);
        printf("&b = %d
    ", &b);
        
        return 0;
    }
    
    // test2.cpp
    #include <stdio.h>
    
    int main()
    {
        int a = 4;
        float& b;
        
        b = 5;
        
        printf("a = %d
    ", a);
        printf("b = %d
    ", b);
        printf("&a = %d
    ", &a);
        printf("&b = %d
    ", &b);
        
        return 0;
    }
    
    // test3.cpp
    #include <stdio.h>
    
    int main()
    {
        int a = 4;
        float& b = 1;
        
        b = 5;
        
        printf("a = %d
    ", a);
        printf("b = %d
    ", b);
        printf("&a = %d
    ", &a);
        printf("&b = %d
    ", &b);
        
        return 0;
    }
    

    3.小结

    • bool类型是C++新增加的基础类型
    • bool类型的值只能是truefalse
    • C++中的三目运算符可作为左值使用
    • C++中的引用可以看作变量的别名来使用
    • 三目运算符的可能返回都是变量时返回的是引用
  • 相关阅读:
    LaTex 章节层次
    计算机组成原理——第二章 运算方法和运算器(笔记)
    计算机组成原理——第一章 计算机组成原理概论(笔记)
    LaTex图片的插入与引用
    adobe 下载与安装
    Jupyter upload无效(无法上传文件)的解决办法
    小星星的js 找不到了 ,直接上源码
    excle网页上预览
    svn is already locked. 文件被锁定
    搭建SSM项目,从数据库到前台数据显示 2019
  • 原文地址:https://www.cnblogs.com/PyLearn/p/10070918.html
Copyright © 2011-2022 走看看