【C/C++】C/C++基本数据类型
2013-05-06 18:05
176 查看
标准C基本数据类型:int char long short float double void以及它们与signed、unsigned的组合。标准C++增加了bool型和wchar_t型,在32位操作系统上,它们的长度如下表:
在不同的平台下,字长不同,具体可以通过以下代码查看:
[cpp] view
plaincopy
#include<stdio.h>
void main()
{
//字符型 有符号和无符号的字节数一样 只是范围不同
printf("char is %d\n",sizeof(char));
printf("unsigned char is %d\n",sizeof(unsigned char));
//整数型
printf("short int is %d\n",sizeof(short int));
printf("unsigned short int is %d\n",sizeof(unsigned short int));
printf("long int is %d\n",sizeof(long int));
printf("int is %d\n",sizeof(int));
printf("unsigned int is %d\n",sizeof(unsigned int));
//浮点型
printf("float is %d\n",sizeof(float));
printf("double is %d\n",sizeof(double));
printf("long double is %d\n",sizeof(long double));
}
在头文件climits.h中,定义了符号常量来表示类型的限制。其列表如下:
* 其精确值依赖于你的系统和库的实现
类型标识符 | 类型说明 | 长度(字节) | 范围 | 备注 |
char | 字符型 | 1 | -128 ~ 127 | -27 ~ (27 -1) |
unsigned char | 无符字符型 | 1 | 0 ~ 255 | 0 ~ (28 -1) |
short int | 短整型 | 2 | -32768 ~ 32767 | 2-15 ~ (215 - 1) |
unsigned short int | 无符短整型 | 2 | 0 ~ 65535 | 0 ~ (216 - 1) |
long int | 长整型 | 4 | -2147483648 ~ 2147483646 | -231 ~ (231 - 0) |
int | 整型 | 4 | -2147483648 ~ 2147483647 | -231 ~ (231 - 1) |
unsigned int | 无符整型 | 4 | 0 ~ 4294967295 | 0 ~ (232-1) |
float | 实型(单精度) | 4 | 1.18*10-38 ~ 3.40*1038 | 7位有效位 |
double | 实型(双精度) | 8 | 2.23*10-308 ~ 1.79*10308 | 15位有效位 |
long double | 实型(长双精度) | 10 | 3.37*10-4932 ~ 1.18*104932 | 19位有效位 |
[cpp] view
plaincopy
#include<stdio.h>
void main()
{
//字符型 有符号和无符号的字节数一样 只是范围不同
printf("char is %d\n",sizeof(char));
printf("unsigned char is %d\n",sizeof(unsigned char));
//整数型
printf("short int is %d\n",sizeof(short int));
printf("unsigned short int is %d\n",sizeof(unsigned short int));
printf("long int is %d\n",sizeof(long int));
printf("int is %d\n",sizeof(int));
printf("unsigned int is %d\n",sizeof(unsigned int));
//浮点型
printf("float is %d\n",sizeof(float));
printf("double is %d\n",sizeof(double));
printf("long double is %d\n",sizeof(long double));
}
在头文件climits.h中,定义了符号常量来表示类型的限制。其列表如下:
name | expresses | value* |
---|---|---|
CHAR_BIT | Number of bits in a charobject (byte) | 8or greater |
SCHAR_MIN | Minimum value for an object of type signed char | -127( -27+1) or less |
SCHAR_MAX | Maximum value for an object of type signed char | 127( 27-1) or greater |
UCHAR_MAX | Maximum value for an object of type unsigned char | 255( 28-1) or greater |
CHAR_MIN | Minimum value for an object of type char | either SCHAR_MIN or 0 |
CHAR_MAX | Maximum value for an object of type char | either SCHAR_MAX or UCHAR_MAX |
MB_LEN_MAX | Maximum number of bytes in a multibyte character, for any locale | 1or greater |
SHRT_MIN | Minimum value for an object of type short int | -32767( -215+1) or less |
SHRT_MAX | Maximum value for an object of type short int | 32767( 215-1) or greater |
USHRT_MAX | Maximum value for an object of type unsigned short int | 65535( 216-1) or greater |
INT_MIN | Minimum value for an object of type int | -32767( -215+1) or less |
INT_MAX | Maximum value for an object of type int | 32767( 215-1) or greater |
UINT_MAX | Maximum value for an object of type unsigned int | 65535( 216-1) or greater |
LONG_MIN | Minimum value for an object of type long int | -2147483647( -231+1) or less |
LONG_MAX | Maximum value for an object of type long int | 2147483647( 231-1) or greater |
ULONG_MAX | Maximum value for an object of type unsigned long int | 4294967295( 232-1) or greater |
LLONG_MIN | Minimum value for an object of type long long int | -9223372036854775807( -263+1) or less |
LLONG_MAX | Maximum value for an object of type long long int | 9223372036854775807( 263-1) or greater |
ULLONG_MAX | Maximum value for an object of type unsigned long long int | 18446744073709551615( 264-1) or greater |
相关文章推荐
- 定义一个不受计算机字长限制的整数类INT,要求INT与INT以及INT与C++基本数据类型int之间能进行+、-、×、÷和=运算,并且能通过cout输出INT类型的值。(持续添加)
- C/C++基本数据类型所占字节数以及sizeof运算符的使用
- C/C++基本数据类型所占字节数
- c++基本数据类型字节长度
- C++与Java基本数据类型比较
- C/C++基本数据类型(转载)
- 【C/C++语言入门篇】-- 基本数据类型
- 逆向基础第一篇:C++中基本数据类型的表现形式
- C/C++基本数据类型的存储空间长度及取值范围
- C/C++基本数据类型
- CPU位数、操作系统位数、计算机字长、C/C++基本数据类型长度
- C++ 中基本数据类型的最大最小值
- C/C++基本数据类型
- C++基本数据类型总结
- java,c,c++ 语言之间基本数据类型的比较
- (转)C/C++基本数据类型长度
- C/C++基本数据类型所占字节数
- 输出C++基本数据类型的数据范围
- The New C++ -- 基本数据类型和字面值常量 (1)
- 16位计算机中C++常用基本数据类型描述 和 字符常量 摘自 钱能C++教程