2、C++零基础学习之数据类型

        摘要:C++规定在创建一个变量、常量时,必须要指定出相应的数据类型,否则无法给分配内存。因此,数据类型存在的意义:类变量分配合适的内存空间。(不同类型占用的内存大小不同)。本章节主要讲解C++中各种数据类型。

目录

        2.1 整型

        2.2 sizeof关键字

        2.3 浮点型

        2.4 字符型

        2.5 转义字符

        2.6 字符串

        2.7 布尔类型

        2.8 数据的输入、输出


2.1 整型

  • C++表示整型有以下几种方式,区别在于所占内存空间不同。
  • 下图,以此能表示的整型数越大

2.2 sizeof关键字

  •         作用:利用sizeof关键字可以统计数据类型所占内存大小
  •         语法:sizeof(变量)。其中:short < int<=long<long long
#include <iostream>
using namespace std;

int main() {    
    short num1 = 10;            // 短整型
    int num2 = 10;              // 整型
    long num3 = 10;             // 长整型
    long long num4 = 10;        // 长长整型
        
    // 输出2,4,4,8
    cout << "num1 = " << sizeof(num1) << endl;        
    cout << "num2 = " << sizeof(num2) << endl;
    cout << "num3 = " << sizeof(num3) << endl;
    cout << "num4 = " << sizeof(num4) << endl;

    system("pause");
    return 0 ;
 }

2.3 浮点型

        浮点型(小数)分为两类:两者的区别表示有效数字范围不同

数据类型

创建规范

占用内存

有效数字范围

单精度float

float 变量名 = 数值f

4字节

7位有效数字

双精度double

double 变量名 = 数值

8字节

15~16位有效数字

#include <iostream>
using namespace std;

int main() {
    float num1 = 3.14f;            // 单精度,
    double num2 = 3.14;            // 双精度型
    
    cout << "num1 = " << sizeof(num1) << endl;
    cout << "num2 = " << sizeof(num2) << endl;

    system("pause");
    return 0;
}

2.4 字符型

        语法:char ch = 'a';

        注意:

  •         字符必须单引号,只能一个字符,不能是字符串。
  •         字符型变量只占用1个字符,‘a’。如果是'abc',则出错
  •         字符型变量不是把字符放进内存中存储,而是将对应的ASCll编码放入存储单元
#include <iostream>
using namespace std;

int main() {
    char ch = 'a';
    cout << "ch = " << ch << endl;
    cout << "ch占用内存:" << sizeof(ch) << endl;
    cout << "ch的ASCll是" << int(ch) << endl;

    system("pause");
    return 0 ;
}

2.5 转义字符

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

        常用的有:\n(换行), \\(输出反斜杠), \t(水平制表符,可以对齐,类似表格)

#include <iostream>
using namespace std;

int main() {
    // 换行字符\n
    cout << "hello world\n " << "换行" << endl;
    // 输出反斜杠
    cout << "\\ " << endl;
    // 水平制表符 \t
    cout << "hello world\thello world "<< endl;

    system("pause");
    return 0 ;
}

2.6 字符串

        字符串创建: string 变量名= “字符串的值”

        注意:必须是双引号

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "abcd";
    cout << "str1 =  " << str1 << endl;

    system("pause");
    return 0;
}

2.7 布尔类型

  •         定义:布尔数据类型代表真或假
  •         语法: bool 变量名 = 布尔值(true/false)
#include <iostream>
using namespace std;

int main() {
    bool str1 = true;    
    // 换行字符\n
    cout << "str1 =  " << str1 << endl;
    cout << "str1占用内存大小:" << sizeof(str1) << endl;

    system("pause");
    return 0;
}

2.8 数据的输入、输出

        作用:用于数据的输入、输出

        关键字:cin、cout

#include <iostream>
using namespace std;

int main() {
// 1、整型
    int a = 0;
    cout << "请给整型变量a赋值:" << endl;
    cin >> a;
    cout << "整型变量a = " << a << endl;

// 2、浮点型
    float b = 3.1f;
    cout << "请给整型变量b赋值:" << endl;
    cin >> b;
    cout << "整型变量b = " << b << endl;

system("pause");
return 0;
}

        

        上一篇:1、C++零基础学习之变量、关键字

        下一篇:3、C++零基础学习之运算符-CSDN博客

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值