C++数据类型

整型

cpp 复制代码
	//1.短整型 (2字节)
    short num1 = 10;
    //2.整型    (4字节)
    int num2 = 10;
    //3.长整型  (4字节)
    long num3 = 10;
    //4.长长整型 (8字节)
    long long num4 = 10;

    cout<<"num1="<<num1<<endl;
    cout<<"num2="<<num2<<endl;
    cout<<"num3="<<num3<<endl;
    cout<<"num4="<<num4<<endl;

    //利用sizeof关键字可以统计数据类型所占内存大小
    cout<<"short类型占空间内存为:"<<sizeof(num1)<<endl;
    cout<<"int类型占空间内存为:"<<sizeof(num2)<<endl;
    cout<<"long类型占空间内存为:"<<sizeof(num3)<<endl;
    cout<<"long long类型占空间内存为:"<<sizeof(num4)<<endl;

浮点型

cpp 复制代码
 //1.单精度float
    //2.双精度 double
    //float占4字节,double占8字节
    //默认情况下,输出一个小数,会显示出六位有效数字
    float f1 = 3.1415926f;
    double d1 = 3.1415926;


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

科学计数法

cpp 复制代码
    //科学计数法
    float f2 = 3e2; //3*10^2
    float f3 = 3e-2;//3*10^-2
    cout<<"f2="<<f2<<endl;
    cout<<"f3="<<f3<<endl;

字符型

cpp 复制代码
    //字符型
    //a的ASCII值为97,b为98
    char ch = 'a';
    char ch2= 'b';
    cout<<ch<<endl;
    cout<<"char字符型变量所占内存:"<<sizeof(char)<<endl;
    cout<< (int)ch <<endl;
    cout<< (int)ch2 <<endl;

转义字符

cpp 复制代码
    //转义字符
    //换行符    \n
    //反斜杠    \\
    //水平制表符\t
    cout<< "aaaaa\thelloworld"<<endl;
    cout<< "aaa\thelloworld"<<endl;
    cout<< "aaaaaaa\thelloworld"<<endl;

字符串

cpp 复制代码
    //字符串
    //1.C风格字符串
    //注意事项:char 字符串名[];等号后面要用双引号包含起来字符串
    char str[] = "hello world";
    cout<< str << endl;
    //2.C++风格字符串
    string str2 = "hello world";
    cout<<str2<<endl;

布尔类型

cpp 复制代码
    //布尔类型,占一字节
    bool flag = true;
    cout<<flag<<endl;

    flag = false;
    cout<<flag<<endl;
    cout<<"size of bool = "<<sizeof(bool)<<endl;
相关推荐
ZC跨境爬虫4 分钟前
3D 地球卫星轨道可视化平台开发 Day12(解决初始相位拥挤问题,实现卫星均匀散开渲染)
前端·javascript·算法·3d·json
青槿吖8 分钟前
告别RestTemplate!Feign让微服务调用像点外卖一样简单
java·开发语言·分布式·spring cloud·微服务·云原生·架构
子午10 分钟前
蔬菜识别~Python+深度学习+卷积网络算法+图像识别+2026原创+蔬菜识别
python·深度学习·算法
chxii10 分钟前
lua 下载和配置环境变量
开发语言·lua
6Hzlia11 分钟前
【Hot 100 刷题计划】 LeetCode 155. 最小栈 | C++ 打包状态法 (最优雅的 O(1) 检索)
java·c++·leetcode
子午12 分钟前
文本情感识别系统~Python+textCNN算法+深度学习+人工智能
人工智能·python·算法
froginwe1122 分钟前
Bootstrap4 导航栏
开发语言
虾神说D23 分钟前
[AI时代码农生存指南]Rust编写CLI 01. CLI的复古轮回
开发语言·人工智能·rust
Kurisu_红莉栖23 分钟前
c++的复习——多态
开发语言·c++
geovindu24 分钟前
go: Prototype Pattern
开发语言·设计模式·golang·原型模式