C++ Learning Log 17--Boolean Types, List Initialization and Coercion


1. Boolean type

#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;


}

insert image description here

The boolean type only contains two values, 0 and 1, std::boolalpha is used to make the output in English (1 becomes true, 2 becomes false)

2. List initialization and forced type conversion

#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;
}

insert image description here

Get the result shown above.

Guess you like

Origin blog.csdn.net/taiyuezyh/article/details/124106732