先看一个例子,貌似有些编译器并不完全支持所有数据类型,运行结果可能也有差异,依编译器而异,但是获得方式都是一样的、、
#include <limits.h>
#include <float.h>
#include <stdlib.h>
int main(void)
{
printf("char类型的变量存储值从%d到%d\n", CHAR_MIN, CHAR_MAX);
printf("unsigned char类型的变量存储值从0到%u\n", UCHAR_MAX);
printf("short类型的变量存储值从%d到%d\n", SHRT_MIN, SHRT_MAX);
printf("unsigned short类型的变量存储值从0到%u\n", USHRT_MAX);
printf("int类型的变量存储值从%d到%d\n", INT_MIN, INT_MAX);
printf("unsigned int类型的变量存储值从0到%u\n", UINT_MAX);
printf("long类型的变量存储值从%ld到%ld\n", LONG_MIN, LONG_MAX);
printf("unsigned long类型的变量存储值从0到%lu\n\n", ULONG_MAX);
printf("long long类型的变量存储值从%lld到%lld\n", LLONG_MIN, LLONG_MAX);
printf("unsigned long long类型的变量存储值从0到%llu\n", ULLONG_MAX);
printf("最小的非零float类型变量的值的是%.3e\n", FLT_MIN);
printf("最大的float类型变量的值的是%.3e\n", FLT_MAX);
printf("最小的非零double类型变量的值的是%.3e\n", DBL_MIN);
printf("最大的double类型变量的值的是%.3e\n\n", DBL_MAX);
printf("最小的非零long double类型变量的值的是%.3Le\n", LDBL_MIN);
printf("最大的long double类型变量的值的是%.3Le\n", LDBL_MAX);
printf("float类型的变量提供%u位精度的小数位数\n", FLT_DIG);
printf("double类型的变量提供%u位精度的小数位数\n\n", DBL_DIG);
printf("long double类型的变量提供%u位精度的小数位数\n", LDBL_DIG);
system("pause");
return 0;
运行结果如下:
char类型的变量存储值从-128到127 unsigned char类型的变量存储值从0到255 short类型的变量存储值从-32768到32767 unsigned short类型的变量存储值从0到65535 int类型的变量存储值从-2147483648到2147483647 unsigned int类型的变量存储值从0到4294967295 long类型的变量存储值从-2147483648到2147483647 unsigned long类型的变量存储值从0到4294967295 long long类型的变量存储值从-9223372036854775808到9223372036854775807 unsigned long long类型的变量存储值从0到18446744073709551615 最小的非零float类型变量的值的是1.175e-038 最大的float类型变量的值的是3.403e+038 最小的非零double类型变量的值的是2.225e-308 最大的double类型变量的值的是1.798e+308 最小的非零long double类型变量的值的是-0.000e+000 最大的long double类型变量的值的是-1.#QOe+000 float类型的变量提供6位精度的小数位数 double类型的变量提供15位精度的小数位数 long double类型的变量提供18位精度的小数位数
C++标准规定,int占一个机器字长。在32位系统中int占32位,也就是4个字节,而在老式的16位系统中,int占16位,即2个字节。而C++标准中只限制规定short int不能超过int的长度,具体长度的可以由C++编译器的实现厂商自行决定。目前流行的32位C++编译器中,通常int占4字节,short int占2字节。其中short int可以简写为short。类似地,C++标准只限制了long int不得小于int的长度,具体也没有作出限制。 无论何时 short 肯定是占两个字节!
关于int 类型与操作系统,编译器相关说明如下:
如:win32下为32位,win64下为64位
如果软件有移植需求,请使用固定大小的数据类型
注意:long 的字节数是不随平台改变的,都是4字节
补充:
BYTE 1字节 typedef unsigned char BYTE;
WORD 2字节 typedef unsigned short WORD;
DWORD 4字节 typedef unsigned long DWORD;
C++中各种数据类型的取值范围记入如下表
数据类型名称 | 字节数 | 别名 | 取值范围 |
int | * | signed,signed int | 由操作系统决定,即与操作系统的"字长"有关 |
unsigned int | * | unsigned | 由操作系统决定,即与操作系统的"字长"有关 |
__int8 | 1 | char,signed char | –128 到 127 |
__int16 | 2 | short,short int,signed short int | –32,768 到 32,767 |
__int32 | 4 | signed,signed int | –2,147,483,648 到 2,147,483,647 |
__int64 | 8 | 无 | –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
bool | 1 | 无 | false 或 true |
char | 1 | signed char | –128 到 127 |
unsigned char | 1 | 无 | 0 到 255 |
short | 2 | short int,signed short int | –32,768 到 32,767 |
unsigned short | 2 | unsigned short int | 0 到 65,535 |
long | 4 | long int,signed long int | –2,147,483,648 到 2,147,483,647 |
long long | 8 | none (but equivalent to __int64) | –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807 |
unsigned long | 4 | unsigned long int | 0 到 4,294,967,295 |
enum | * | 无 | 由操作系统决定,即与操作系统的"字长"有关 |
float | 4 | 无 | 3.4E +/- 38 (7 digits) |
double | 8 | 无 | 1.7E +/- 308 (15 digits) |
long double | 8 | 无 | 1.7E +/- 308 (15 digits) |
wchar_t | 2 | __wchar_t | 0 到 65,535 |
原文链接:http://www.cnblogs.com/ma6174/archive/2012/02/04/2337958.html
http://blog.163.com/lj_2005/blog/static/4586542201131225724/