标准C基本数据类型:int char long short float double void以及它们与signed、unsigned的组合。标准C++增加了bool型和wchar_t型,在32位操作系统上,它们的长度如下表:
类型标识符 | 类型说明 | 长度(字节) | 范围 | 备注 |
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位有效位 |
在不同的平台下,字长不同,具体可以通过以下代码查看:
- #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 char object (byte) |
8 or 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 | 1 or 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 |
* 其精确值依赖于你的系统和库的实现