标准C基本数据类型:int char long short float double void以及它们与signed、unsigned的组合。标准C++增加了bool型和wchar_t型,在32位操作系统上,它们的长度如下表:
类型标识符
类型说明
长度(字节)
范围
备注
char
字符型
1
-128 ~ 127
-27 ~ (27 -1)
unsigned char
无符字符型
0 ~ 255
0 ~ (28 -1)
short int
短整型
2
-32768 ~ 32767
2-15 ~ (215 - 1)
unsigned short int
无符短整型
0 ~ 65535
0 ~ (216 - 1)
long int
长整型
4
-2147483648 ~ 2147483646
-231 ~ (231 - 0)
int
整型
-2147483648 ~ 2147483647
-231 ~ (231 - 1)
unsigned int
无符整型
0 ~ 4294967295
0 ~ (232-1)
float
实型(单精度)
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 <code>char</code> object (byte)
<code>8</code> or greater
SCHAR_MIN
Minimum value for an object of type <code>signed char</code>
<code>-127</code> (<code>-27+1</code>) or less
SCHAR_MAX
Maximum value for an object of type <code>signed char</code>
<code>127</code> (<code>27-1</code>) or greater
UCHAR_MAX
Maximum value for an object of type <code>unsigned char</code>
<code>255</code> (<code>28-1</code>) or greater
CHAR_MIN
Minimum value for an object of type <code>char</code>
either SCHAR_MIN or <code>0</code>
CHAR_MAX
Maximum value for an object of type <code>char</code>
either SCHAR_MAX or UCHAR_MAX
MB_LEN_MAX
Maximum number of bytes in a multibyte character, for any locale
<code>1</code> or greater
SHRT_MIN
Minimum value for an object of type <code>short int</code>
<code>-32767</code> (<code>-215+1</code>) or less
SHRT_MAX
Maximum value for an object of type <code>short int</code>
<code>32767</code> (<code>215-1</code>) or greater
USHRT_MAX
Maximum value for an object of type <code>unsigned short int</code>
<code>65535</code> (<code>216-1</code>) or greater
INT_MIN
Minimum value for an object of type <code>int</code>
INT_MAX
Maximum value for an object of type <code>int</code>
UINT_MAX
Maximum value for an object of type <code>unsigned int</code>
LONG_MIN
Minimum value for an object of type <code>long int</code>
<code>-2147483647</code> (<code>-231+1</code>) or less
LONG_MAX
Maximum value for an object of type <code>long int</code>
<code>2147483647</code> (<code>231-1</code>) or greater
ULONG_MAX
Maximum value for an object of type <code>unsigned long int</code>
<code>4294967295</code> (<code>232-1</code>) or greater
LLONG_MIN
Minimum value for an object of type <code>long long int</code>
<code>-9223372036854775807</code> (<code>-263+1</code>) or less
LLONG_MAX
Maximum value for an object of type <code>long long int</code>
<code>9223372036854775807</code> (<code>263-1</code>) or greater
ULLONG_MAX
Maximum value for an object of type <code>unsigned long long int</code>
<code>18446744073709551615</code> (<code>264-1</code>) or greater
* 其精确值依赖于你的系统和库的实现