手把手带你学习C++的数据类型

目录

  • 数据类型
    • 01 整型:
    • 02 sizeof关键字
    • 03 实型(浮点型)
    • 04 字符型
    • 05 转义字符
    • 06 字符串型
    • 07 布尔型
    • 08 数据的输入
  • 总结

    数据类型 C++规定在创建一个变量或者常量时,必须要指定相应的数据类型,否则无法给变量分配内存空间。

    01 整型:
    数据类型 占用空间 取值范围
    short(短整型) 2字节 -2^15~2^15-1
    int(整型) 4字节 -2^31~2^31-1
    long(长整型) 4字节/8字节 -2^31~2^31-1
    long long(长长整型) 8字节 -2^63~2^63-1
    #includeusing namespace std; int main1(){ //整型 //1.短整型 short num1 = 10; //short num1 = 32768; //2.整型 int num2 = 10; //3.长整型 long num3 = 10; //4.长长整型 long long num4 = 10; cout << num1 << endl; cout << num2 << endl; cout << num3 << endl; cout << num4 << endl; system("pause"); return 0; }


    02 sizeof关键字
    作用:利用sizeof关键字可以统计数据类型所占内存大小。
    语法:sizeof(数据类型/变量)
    #includeusing namespace std; int main2(){ //整型:short(2) int(4) long(4) long long(8) //可以利用sizeof求出数据类型占用内存大小 //语法:sizeof(数据类型/变量) short num1 = 10; cout << "short占用内存空间为:" << sizeof(short) << endl; //cout << "short占用内存空间为:" << sizeof(num1) << endl; int num2 = 10; cout << "int占用内存空间为:" << sizeof(int) << endl; //cout << "int占用内存空间为:" << sizeof(num2) << endl; long num3 = 10; cout << "long占用内存空间为:" << sizeof(long) << endl; //cout << "long占用内存空间为:" << sizeof(num3) << endl; long long num4 = 10; cout << "long long占用内存空间为:" << sizeof(long long) << endl; //cout << "long long占用内存空间为:" << sizeof(num4) << endl; //整型大小比较 //short < int <= long <= long long system("pause"); return 0; }


    03 实型(浮点型)
    作用:用于表示小数。
    【手把手带你学习C++的数据类型】浮点型变量分为两种:float和double,区别在于表示的数字范围不同。
    数据类型 占用空间 有效数字范围
    float 4字节 7位有效数字
    double 8字节 15~16位有效数字
    #includeusing namespace std; int main3(){ //实型 //1.单精度 float //2.双精度 double //默认情况下,输出一个小数,会显示出6位有效数字 float f1 = 3.1415926f; double d1 = 3.1415926; cout << "f1 = " << f1 << endl; cout << "d1 = " << d1 << endl; //float占用4字节内存空间,double占用8字节 cout << "float占用的内存空间为:" << sizeof(float) << endl; cout << "double占用的内存空间为:" << sizeof(double) << endl; //科学计数法 float f2 = 3e2; //3*10^2,300 cout << "f2 = " << f2 << endl; float f3 = 3e-2; //3*0.1^2,0.03 cout << "f3 = " << f3 << endl; system("pause"); return 0; }


    04 字符型
    作用:用于显示单个字符。
    语法:char ch = 'a';
    注意1:在显示字符串变量时,用单引号将字符括起来,不要用双引号。
    注意2:单引号内只能有一个字符,不可以是字符串。
    C和C++中字符串变量只占用1个字节。
    字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元中。
    #includeusing namespace std; int main4(){ //1.字符型变量创建方式 char ch = 'a'; cout << ch << endl; //2.字符型变量所占内存大小 cout << "char字符型变量所占内存为:" << sizeof(char) << endl; //3.字符型变量常见错误 //char ch2 = "b"; 创建字符型变量时,要用单引号 //char ch2 = 'abcdefg'; 创建字符型变量时,单引号内只能有一个字符 //4.字符型变量对应ASCII编码 //a - 97 A - 65 cout << (int)ch << endl; system("pause"); return 0; }


    05 转义字符
    作用:用于表示一些不能显示出来的ASCII字符。
    \n 换行,将当前位置移到下一行开头
    \t 水平制表,跳到下一个TAB位置
    \\ 代表一个反斜线字符'\'
    #includeusing namespace std; int main5(){ //转义字符 //换行符\n cout << "Hello world" << endl; cout << "Hello world\n"; //反斜杠\\ cout << "\\" << endl; //水平制表符\t cout << "aaaa\thelloworld" << endl; cout << "aa\thelloworld" << endl; cout << "aaaaaa\thelloworld" << endl; system("pause"); return 0; }


    06 字符串型
    C风格字符串:char 变量名[] = "字符串值"
    C++风格字符串:string 变量名 = "字符串值"
    #include#include //用C++风格字符串时,要包含这个头文件using namespace std; int main6(){ //1.C风格字符串 //注意事项1:char字符串名后加[] //注意事项2:等号后面要用双引号,包含起来字符串 char str[] = "hello world"; cout << str << endl; //2.C++风格字符串 string str2 = "hello world"; cout << str2 << endl; system("pause"); return 0; }


    07 布尔型
    作用:布尔数据类型代表真或者假的值。
    只有两个值:True或者False,占用1字节大小的内存空间。
    #includeusing namespace std; int main7(){ //1.创建bool数据类型 bool flag = true; cout << flag << endl; flag = false; cout << flag << endl; //1代表真,0代表假 //2.查看bool类型所占内存空间 cout << "bool类型所占内存空间:" << sizeof(bool) << endl; system("pause"); return 0; }


    08 数据的输入
    作用:从键盘获取数据。
    关键字:cin
    语法:cin >> 变量
    #include#includeusing namespace std; int main(){ //1.整型 /*int a = 0; cout << "请给整型变量a赋值:" << endl; cin >> a; cout << "整型变量a = " << a << endl; */ //2.浮点型 /*float f = 3.14f; cout << "请给浮点型变量f赋值:" << endl; cin >> f; cout << "浮点型变量f = " << f << endl; */ //3.字符型 /*char ch = 'a'; cout << "请给字符型变量ch赋值:" << endl; cin >> ch; cout << "字符型变量ch = " << ch << endl; */ //4.字符串型 /*string str = "hello"; cout << "请给字符串型变量str赋值:" << endl; cin >> str; cout << "字符串型变量str = " << str << endl; */ //5.布尔型 bool flag = false; cout << "请给布尔型变量flag赋值:" << endl; cin >> flag; cout << "布尔型变量flag = " << flag << endl; system("pause"); return 0; }


    总结
    本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

      推荐阅读