C++学习日志17--布尔类型、列表初始化与强制类型转化


一、布尔类型

#include <iostream>
int main()
{
    
    

    bool isAlpha;
    isAlpha = false;
    if (!isAlpha)
    {
    
    
        std::cout << "isAlpha=" << isAlpha << std::endl;
        std::cout << std::boolalpha <<
            "isAlpha=" << isAlpha << std::endl;

    }
    return 0;


}

在这里插入图片描述

布尔类型只包含0和1两个值,std::boolalpha用于使得输出为英文(1变为true、2变为false)

二、列表初始化与强制类型转化

#include <iostream>
int main()
{
    
    
    //列表初始化不允许窄化
    int x{
    
     1 };
    std::cout << x << std::endl;

    //强制类型转换 :int ->double;double->int
    std::cout << 1 / 2 << std::endl;
    std::cout << static_cast<double>(1) / 2 << std::endl; 
    std::cout << static_cast<double>(1 / 2) << std::endl;


    std::cout << 1.0f / 2.f << std::endl;
    std::cin.get();

    return 0;
}

在这里插入图片描述

得到上图所示的结果。

猜你喜欢

转载自blog.csdn.net/taiyuezyh/article/details/124106732
今日推荐