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;
相关推荐
蒹葭玉树2 分钟前
【C++上岸】C++常见面试题目--操作系统篇(第二十八期)
linux·c++·面试
醉颜凉2 分钟前
【LeetCode】打家劫舍III
c语言·算法·leetcode·树 深度优先搜索·动态规划 二叉树
qq_177767373 分钟前
React Native鸿蒙跨平台数据使用监控应用技术,通过setInterval每5秒更新一次数据使用情况和套餐使用情况,模拟了真实应用中的数据监控场景
开发语言·前端·javascript·react native·react.js·ecmascript·harmonyos
达文汐5 分钟前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
一匹电信狗5 分钟前
【LeetCode_21】合并两个有序链表
c语言·开发语言·数据结构·c++·算法·leetcode·stl
User_芊芊君子5 分钟前
【LeetCode经典题解】搞定二叉树最近公共祖先:递归法+栈存路径法,附代码实现
算法·leetcode·职场和发展
算法_小学生6 分钟前
LeetCode 热题 100(分享最简单易懂的Python代码!)
python·算法·leetcode
执着2597 分钟前
力扣hot100 - 234、回文链表
算法·leetcode·链表
Gorgous—l8 分钟前
数据结构算法学习:LeetCode热题100-多维动态规划篇(不同路径、最小路径和、最长回文子串、最长公共子序列、编辑距离)
数据结构·学习·算法
熬夜造bug9 分钟前
LeetCode Hot100 刷题路线(Python版)
算法·leetcode·职场和发展