C++程序查找系统中int、float、double和char的大小


C++中的数据类型

C++中有很多数据类型,但最常用的包括int、float、double和char。以下是关于这些数据类型的详细信息:

  • int - 用于存储整型数据,通常需要4个字节的内存空间。

  • float - 用于存储单精度浮点数或小数。float变量通常需要4个字节的内存空间。

  • double - 用于存储双精度浮点数或小数。double变量通常需要8个字节的内存空间。

  • char - 用于存储字符。字符通常需要1个字节的内存空间。

C++中的sizeof运算符

sizeof运算符用于查找数据类型的大小。它是一个编译时运算符,用于确定不同变量和数据类型以字节为单位的大小。sizeof运算符的语法如下:

sizeof (data type);

以下是一个查找int、float、double和char大小的程序:

示例

 实时演示

#include <iostream>
using namespace std;
int main() {
   cout<<"Size of int is "<<sizeof(int)<<" bytes"<<endl;
   cout<<"Size of float is "<<sizeof(float)<<" bytes"<<endl;
   cout<<"Size of double is "<<sizeof(double)<<" bytes"<<endl;
   cout<<"Size of char is "<<sizeof(char)<<" byte"<<endl;
   return 0;
}

输出

Size of int is 4 bytes
Size of float is 4 bytes
Size of double is 8 bytes
Size of char is 1 byte

在上述程序中,sizeof运算符用于查找int、float、double和char的大小。这使用cout对象显示出来。

cout<<"Size of int is "<<sizeof(int)<<" bytes"<<endl;
cout<<"Size of float is "<<sizeof(float)<<" bytes"<<endl;
cout<<"Size of double is "<<sizeof(double)<<" bytes"<<endl;
cout<<"Size of char is "<<sizeof(char)<<" byte"<<endl;

更新于: 2020年6月23日

3K+ 浏览量

开启你的职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.