编写程序输出每种内置类型的长度

来源:互联网 发布:大嘴猴淘宝旗舰店 编辑:程序博客网 时间:2024/06/04 19:17

程序如下:

int main(/*int argc, char *argv[]*/){cout << "type\t\t\t" << "size" << endl<< "bool\t\t\t" << sizeof(bool) << endl<< "char\t\t\t" << sizeof(char) << endl<< "signed char\t\t" << sizeof(signed char) << endl<< "unsigned char\t\t" << sizeof(unsigned char) << endl<< "wchar_t\t\t\t" << sizeof(wchar_t) << endl<< "short\t\t\t" << sizeof(short) << endl<< "signed short\t\t" << sizeof(signed short) << endl<< "unsigned short\t\t" << sizeof(unsigned short) << endl<< "int\t\t\t" << sizeof(int) << endl<< "signed int\t\t" << sizeof(signed int) << endl<< "unsigend int\t\t" << sizeof(unsigned int) << endl<< "long\t\t\t" << sizeof(long) << endl<< "sigend long\t\t" << sizeof(signed long) << endl<< "unsigned long\t\t" << sizeof(unsigned long) << endl<< "float\t\t\t" << sizeof(float) << endl<< "double\t\t\t" << sizeof(double) << endl<< "long double\t\t" << sizeof(long double) << endl;return 0;}

结果如下:

type                    size
bool                    1
char                    1
signed char             1
unsigned char           1
wchar_t                 2
short                   2
signed short            2
unsigned short          2
int                     4
signed int              4
unsigend int            4
long                    4
sigend long             4
unsigned long           4
float                   4
double                  8
long double             8

注:

size为字节的长度,而一字节为8位。

以short型为例,上面输出short型为2字节,那就是16位,而存储的数据范围为 -2^15 ~ 2^15-1。

0 0
原创粉丝点击