您的位置:首页 > 编程语言 > C语言/C++

【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位操作系统上,它们的长度如下表:

类型标识符类型说明长度(字节)范围备注
char字符型1-128 ~ 127-27 ~ (27 -1)
unsigned char无符字符型10 ~ 2550 ~ (28 -1)
short int短整型2-32768 ~ 327672-15 ~ (215 - 1)
unsigned short int无符短整型20 ~ 655350 ~ (216 - 1)
long int长整型4-2147483648 ~ 2147483646-231 ~ (231 - 0)
int整型4-2147483648 ~ 2147483647-231 ~ (231 - 1)
unsigned int无符整型40 ~ 42949672950 ~ (232-1)
float实型(单精度)41.18*10-38 ~ 3.40*10387位有效位
double实型(双精度)82.23*10-308 ~ 1.79*1030815位有效位
long double实型(长双精度)103.37*10-4932 ~ 1.18*10493219位有效位
在不同的平台下,字长不同,具体可以通过以下代码查看:

[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中,定义了符号常量来表示类型的限制。其列表如下:

nameexpressesvalue*
CHAR_BITNumber of bits in a
char
object (byte)
8
or greater
SCHAR_MINMinimum value for an object of type
signed char
-127
(
-27+1
) or less
SCHAR_MAXMaximum value for an object of type
signed char
127
(
27-1
) or greater
UCHAR_MAXMaximum value for an object of type
unsigned char
255
(
28-1
) or greater
CHAR_MINMinimum value for an object of type
char
either SCHAR_MIN or
0
CHAR_MAXMaximum value for an object of type
char
either SCHAR_MAX or UCHAR_MAX
MB_LEN_MAXMaximum number of bytes in a multibyte character, for any locale
1
or greater
SHRT_MINMinimum value for an object of type
short int
-32767
(
-215+1
) or less
SHRT_MAXMaximum value for an object of type
short int
32767
(
215-1
) or greater
USHRT_MAXMaximum value for an object of type
unsigned short int
65535
(
216-1
) or greater
INT_MINMinimum value for an object of type
int
-32767
(
-215+1
) or less
INT_MAXMaximum value for an object of type
int
32767
(
215-1
) or greater
UINT_MAXMaximum value for an object of type
unsigned int
65535
(
216-1
) or greater
LONG_MINMinimum value for an object of type
long int
-2147483647
(
-231+1
) or less
LONG_MAXMaximum value for an object of type
long int
2147483647
(
231-1
) or greater
ULONG_MAXMaximum value for an object of type
unsigned long int
4294967295
(
232-1
) or greater
LLONG_MINMinimum value for an object of type
long long int
-9223372036854775807
(
-263+1
) or less
LLONG_MAXMaximum value for an object of type
long long int
9223372036854775807
(
263-1
) or greater
ULLONG_MAXMaximum value for an object of type
unsigned long long int
18446744073709551615
(
264-1
) or greater
* 其精确值依赖于你的系统和库的实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: