C++自学笔记(数据类型篇)

    科技2022-07-13  118

    C++自学笔记(数据类型篇)

    define 和 const修饰的变量无法被修改 const int a = 10; //const修饰之后,a无法被修改 cout << "a = " << a << endl; << a = 10

    数据类型

    整性

    short(短整型) 两个字节int(整性) 四个字节long (长整型) Windows为4个字节,Linux为4字节或8字节long long (长长整性) 8个字节

    sizeof关键字

    求出数据类型所占空间大小 int a = 10; cout << "a所占空间大小为 " << sizeof(a) << endl; << a所占空间大小为 4

    浮点型

    float (单精度) 4个字节,定义float时在数字后面加个f,才是float类型double(双精度) 8个字节c++默认输出小数显示六位有效数字科学计数法 : 3e2 < => 3 * 10^2

    字符型

    使用char关键字,用单引号括住,只能是字符,不能字符串只占用一个1个字节,存储的是ASCII码

    转义字符

    \n 换行\t 跳到下一个tab位置\\ 代表一个 \

    字符串型

    c语言风格 char 变量名[] = “字符串值”c++语言风格 string 变量名 = “字符串值” 包含头文件include #include<string> //c++输出字符串类型 #include<iostream> using namespace std; int main() { char str1[] = "hello world "; string str2 = "world world"; cout << str1 << endl; cout << str2 << endl; system("pause"); return 0; }

    布尔类型

    bool 变量名 = true or false代表的值是0 或 1占用一个字节

    数据的输入

    作用:用于键盘获取数据关键字:cin语法:cin >> 变量 string str2; cout << "请给str2赋值" << endl; cin >> str2; //注意箭头方向 cout <<"str2的值是:"<< str2 << endl; >>请给str2赋值 >>world >>str2的值是:world
    Processed: 0.012, SQL: 8