C++数据类型

整型

	//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;

浮点型

 //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;

科学计数法

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

字符型

    //字符型
    //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;

转义字符

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

字符串

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

布尔类型

    //布尔类型,占一字节
    bool flag = true;
    cout<<flag<<endl;

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

相关推荐

  1. C语言数据类型

    2024-04-10 12:48:04       33 阅读
  2. c++数据类型解释

    2024-04-10 12:48:04       27 阅读
  3. C++——数据类型笔记

    2024-04-10 12:48:04       27 阅读
  4. C++数据类型

    2024-04-10 12:48:04       12 阅读
  5. C++——数据类型笔记

    2024-04-10 12:48:04       10 阅读
  6. C# 各数据类型 范围

    2024-04-10 12:48:04       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-10 12:48:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-10 12:48:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-10 12:48:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-10 12:48:04       20 阅读

热门阅读

  1. LeetCode //C - 275. H-Index II

    2024-04-10 12:48:04       14 阅读
  2. python蓝桥杯选数

    2024-04-10 12:48:04       14 阅读
  3. Hugging Face Transformers 微调--利用 SQuAD 做问答任务

    2024-04-10 12:48:04       11 阅读
  4. websocket调用http接口

    2024-04-10 12:48:04       13 阅读
  5. 为什么K8s需要服务网格Istio?

    2024-04-10 12:48:04       12 阅读
  6. 【御控物联】 2、物联网构成

    2024-04-10 12:48:04       11 阅读
  7. systemctl stop与信号

    2024-04-10 12:48:04       10 阅读
  8. 一篇文章说清楚 golang之interface

    2024-04-10 12:48:04       11 阅读
  9. 汽车变速器原理?

    2024-04-10 12:48:04       10 阅读