基本变量类型

来源:互联网 发布:mac储存空间正在计算 编辑:程序博客网 时间:2024/05/17 23:33

整型

char,short,int,long

 

short: 至少16bit2byte

int:   至少跟short一样。

long: 至少32bit4byte,且至少跟int一样。

char: 一个字符的长度,8bit1byte

 

//Test the computer's integer limits

#include <iostream>

using namespace std;

#include <climits>

int main()

{

   int n_int = INT_MAX;

   short n_short = SHRT_MAX;

   long n_long = LONG_MAX;

 

   cout << "short is " << sizeof(short) << " bytes./n";

   cout << "int is " << sizeof(int) << " bytes./n";

   cout << "long is " << sizeof(long) << " bytes./n";

 

   cout << "Max Values:/n";

   cout << "short: "<< n_short << endl;

   cout << "int: "<< n_int << endl;

   cout << "long: "<< n_long << endl;

 

   return 0;

}

 

结果:(32位机)

short is 2 bytes

int is 4 bytes.

long is 4 bytes.

Max Values:

short: 32767

int: 2147483647

long: 2147483647

 

16位机上的运算结果应该不同。

变量的长度和取值范围取决于硬件,所以个人认为基本不需要记住所谓的变量长度,

想要知道的时候到使用的环境中测试一下就可以了,或者硬件手册也会记载的吧~~

 

浮点类型

floatdoublelong double

Float:           至少32位,通常32

Doble          至少48位,且不少于float,通常64

Long double    至少和double一样多,通常8096128

指数范围:        至少-3737

 

浮点数和整数:

优点: 可以表示整数之间的数

       可以表示的范围比整数大的多

缺点:浮点运算速度比整数慢

:老版本的C++在显示浮点时,显示6位小数;标准C++在默认情况下总共显示6位。

 

 

 

 

 

原创粉丝点击