«

2 数据类型

时间:2023-9-16 23:28     作者:wen     分类: C++


[toc]

数据类型

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

整型

作用:整型变量表示是整数类型的数据。

C++能够表示整型的类型有以下几种方式,区别在于所占内存空间不同。

数据类型 占用空间 取值范围
short(短整型) 2字节 (-2^15 ~ 2^15-1)
int(整型) 4字节 (-2^31 ~ 2^31-1)
long(长整型) win为4字节,Linux为4字节(32位),8字节(64位) (-2^31 ~ 2^31-1)
long long(长长整型) 8字节 (-2^63 ~ 2^63-1)

sizeof关键字

作用:利用sizeof关键字可以统计数据类型所占的空间大小。

语法:sizeof(数据类型 | 变量)

示例:

#include <iostream>

using namespace std;

int main()
{

    cout << "short 类型所占用的内存空间为:" << sizeof(short) << endl;
    cout << "int 类型所占用的内存空间为:" << sizeof(int) << endl;
    cout << "long 类型所占用的内存空间为:" << sizeof(long) << endl;
    cout << "long long 类型所占用的内存空间为:" << sizeof(long long) << endl;
    system("pause");

    return 0;
}
short 类型所占用的内存空间为:2
int 类型所占用的内存空间为:4
long 类型所占用的内存空间为:4
long long 类型所占用的内存空间为:8
请按任意键继续. . .

实型(浮点型)

作用:用于表示小数。

浮点型变量分为两种:

  1. 单精度float
  2. 双精度double

两者的区别在于表示的有效数值范围不同。

数据类型 占用空间 有效数字范围
float 4字节 7一种叫位有效数字。
double 8字节 15~16位有效数字。

示例:

#include <iostream>

using namespace std;

int main()
{
    // 默认情况下输出一个小数会显示出六位有效数字。
    float f1 = 3.1415926f;
    double d1 = 3.1415926;

    cout << f1 << endl;
    cout << d1 << endl;

    cout << "float 类型所占用的内存空间为:" << sizeof(f1) << endl;
    cout << "double 类型所占用的内存空间为:" << sizeof(d1) << endl;

    // 科学计数法
    float  f2 = 3e2; // 3*10^2
    cout << f2 << endl;

    float  f3 = 3e-2;
    cout << f3 << endl;
    system("pause");

    return 0;
}

字符型

作用:字符型变量用于显示单个字符。

语法:char ch = 'a'

注意1:在显示字符型变量时,用单引号将字符括起来,不要用双引号。

注意2:单引号内只能有一个字符,不能是字符串。

示例:

#include <iostream>

using namespace std;

int main()
{

    char ch = 'a';
    cout << ch << endl;
    cout << sizeof(ch) << endl;

    // 字符型变量对应的ASCII编码
    cout << (int)ch << endl;
    system("pause");

    return 0;
}

转义字符

作用:用于表示一些不能显示出来的ASCII字符

现阶段我们常用的转义字符有:\n \\ \t

转义字符 描述
\\ 反斜杠
\' 单引号
\" 双引号
\a 响铃 (Alert)
\b 退格 (Backspace)
\f 换页符 (Form Feed)
\n 换行符 (Newline)
\r 回车符 (Carriage Return)
\t 制表符 (Tab)
\v 垂直制表符 (Vertical Tab)
\? 问号
\\ 反斜杠
\0 空字符 (Null)
\xhh 十六进制数 (hh 表示两个十六进制数字)
\ooo 八进制数 (ooo 表示三个八进制数字)
#include <iostream>

using namespace std;

int main()
{

    // 转义字符

    // 换行符 \n

    cout << "hello world" << "\n";

    // 反斜杠 \\

    cout << "\\" << endl;

    // 水平制表符 \t 作用可以整齐的输出数据

    cout << "aaaaaa\tbbb" << endl;
    cout << "aaa\tbbb" << endl;
    cout << "aaaa\tbbb" << endl;

    system("pause");

    return 0;
}
hello world
\
aaaaaa  bbb
aaa     bbb
aaaa    bbb
请按任意键继续. . .

字符串

作用:用于表示一串字符

两种风格

  1. C风格字符串:char 变量名[] = "字符串值"

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
       char str1[] = "hello world";
       cout << str1 << endl;
    
       system("pause");
    
       return 0;
    }

    注意:C风格的字符串要用双引号括起来。

  2. C++风格字符串:string 变量名 = "字符串值"

    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
       string str1 = "hello world";
       cout << str1 << endl;
    
       system("pause");
    
       return 0;
    }

    注意:C++风格字符串,需要加入头文件#include <string>,我的这个版本19好像不用加


布尔类型bool

作用:布尔数据类型代表真或假的值

bool类型只有两个值:

bool类型占用1个字节大小

只要是非0的值都是真

示例:

#include <iostream>

using namespace std;

int main()
{

    bool flag = true;
    cout << flag << endl;

    flag = false;
    cout << flag << endl;

    cout << "size of bool = " << sizeof(flag) << endl;

    system("pause");

    return 0;
}

数据的输入

作用:用于从键盘获取数据

关键词:cin

语法: cin >> 变量

示例:

#include <iostream>

using namespace std;

int main()
{
    int score = 0;
    cout << "输入一个整数" << endl;
    cin >> score;
    cout << "您输入的数值为:" << score << endl;

    system("pause");

    return 0;
}

标签: C/C++