-
各种数据类型的取值范围
2020-07-16 16:17:18各种数据类型的取值范围速查表、详细教程与注意事项。 -
C语言各种数据类型的取值范围
2017-03-04 19:46:32速查表: ...short -32767 ~ + 32768 (2 Bytes) -2的15次方-1 ~ 2的15次方 unsigned short 0 ~ 65536 (2 Bytes) 0 ~ 2的16次方 int (long) -2147483648 ~ +2147483647 (4 Bytes) -2的31次方+1 ~速查表:
char -128 ~ +127 (1 Byte)
short -32767 ~ + 32768 (2 Bytes) -2的15次方-1 ~ 2的15次方
unsigned short 0 ~ 65536 (2 Bytes) 0 ~ 2的16次方
int (long) -2147483648 ~ +2147483647 (4 Bytes) -2的31次方+1 ~ 2的31次方
unsigned int 0 ~ 4294967295 (4 Bytes) 0 ~ 2的32次方+1
long == int
long long(__int64) -9223372036854775808 ~ +9223372036854775807 (8 Bytes) -2的63次方+1 ~ 2的63次方unsigned long long的最大值:1844674407370955161
unsigned __int64的最大值:18446744073709551615
long long == __int64
double 1.7 * 10^308 (8 Bytes)
====================================
符号属性 长度属性 基本型 所占位数 取值范围 输入符举例 输出符举例
-- -- char 8 -2^7 ~ 2^7-1 %c %c 、 %d 、 %u
signed -- char 8 -2^7 ~ 2^7-1 %c %c 、 %d 、 %u
unsigned -- char 8 0 ~ 2^8-1 %c %c 、 %d 、 %u
[signed] short [int] 16 -2^15 ~ 2^15-1 %hd
unsigned short [int] 16 0 ~ 2^16-1 %hu 、 %ho 、 %hx
[signed] -- int 32 -2^31 ~ 2^31-1 %d
unsigned -- [int] 32 0 ~ 2^32-1 %u 、 %o 、 %x
[signed] long [int] 32 -2^31 ~ 2^31-1 %ld
unsigned long [int] 32 0 ~ 2^32-1 %lu 、 %lo 、 %lx
[signed] long long [int] 64 -2^63 ~ 2^63-1 %I64d
unsigned long long [int] 64 0 ~ 2^64-1 %I64u 、 %I64o 、 %I64x
-- -- float 32 +/- 3.40282e+038 %f 、 %e 、 %g
-- -- double 64 +/- 1.79769e+308 %lf 、 %le 、 %lg %f 、 %e 、 %g
-- long double 96 +/- 1.79769e+308 %Lf 、 %Le 、 %Lg
几点说明:
1. 注意 ! 表中的每一行,代表一种基本类型。 “[]” 代表可省略。
例如: char 、 signed char 、 unsigned char 是三种互不相同的类型;
int 、 short 、 long 也是三种互不相同的类型。
2. char/signed char/unsigned char 型数据长度为 1 字节;
char 为有符号型,但与 signed char 是不同的类型。
注意 ! 并不是所有编译器都这样处理, char 型数据长度不一定为 1 字节, char 也不一定为有符号型。
3. 将 char/signed char 转换为 int 时,会对最高符号位 1 进行扩展,从而造成运算问题。
所以 , 如果要处理的数据中存在字节值大于 127 的情况,使用 unsigned char 较为妥当。
程序中若涉及位运算,也应该使用 unsigned 型变量。
4. char/signed char/unsigned char 输出时,使用格式符 %c (按字符方式); 或使用 %d 、 %u 、 %x/%X 、 %o ,按整数方式输出; 输入时,应使用 %c ,若使用整数方式, Dev-C++ 会给出警告,不建议这样使用。
5. int 的长度,是 16 位还是 32 位,与编译器字长有关。
16 位编译器(如 TC 使用的编译器)下, int 为 16 位; 32 位编译器(如 VC 使用的编译器 cl.exe )下, int 为 32位。
6. 整型数据可以使用 %d (有符号 10 进制)、 %o (无符号 8 进制)或 %x/%X (无符号 16 进制)方式输入输出。 而格式符 %u ,表示 unsigned ,即无符号 10 进制方式。
7. 整型前缀 h 表示 short , l 表示 long 。
输入输出 short/unsigned short 时,不建议直接使用 int 的格式符 %d/%u 等,要加前缀 h 。这个习惯性错误,来源于 TC 。 TC 下, int 的长度和默认符号属性,都与 short 一致,于是就把这两种类型当成是相同的,都用 int 方式进行输入输出。
8. 关于 long long 类型的输入输出:
"%lld" 和 "%llu" 是 Linux 下 gcc/g++ 用于 long long int 类型 (64 bits) 输入输出的格式符。
而 "%I64d" 和 "%I64u" 则是 Microsoft VC++ 库里用于输入输出 __int64 类型的格式说明。
Dev-C++ 使用的编译器是 Mingw32 , Mingw32 是 x86-win32 gcc 子项目之一,编译器核心还是 linux 下的 gcc 。
进行函数参数类型检查的是在编译阶段, gcc 编译器对格式字符串进行检查,显然它不认得 "%I64d" ,
所以将给出警告 “unknown conversion type character `I' in format” 。对于 "%lld" 和 "%llu" , gcc 理所当然地接受了。
Mingw32 在编译期间使用 gcc 的规则检查语法,在连接和运行时使用的却是 Microsoft 库。
这个库里的 printf 和 scanf 函数当然不认识 linux gcc 下 "%lld" 和 "%llu" ,但对 "%I64d" 和 "%I64u" ,它则是 乐意接受,并能正常工作的。
9. 浮点型数据输入时可使用 %f 、 %e/%E 或 %g/%G , scanf 会根据输入数据形式,自动处理。
输出时可使用 %f (普通方式)、 %e/%E (指数方式)或 %g/%G (自动选择)。
10. 浮点参数压栈的规则: float(4 字节 ) 类型扩展成 double(8 字节 ) 入栈。
所以在输入时,需要区分 float(%f) 与 double(%lf) ,而在输出时,用 %f 即可。
printf 函数将按照 double 型的规则对压入堆栈的 float( 已扩展成 double) 和 double 型数据进行输出。
如果在输出时指定 %lf 格式符, gcc/mingw32 编译器将给出一个警告。
11. Dev-C++(gcc/mingw32) 可以选择 float 的长度,是否与 double 一致。
12. 前缀 L 表示 long ( double )。
虽然 long double 比 double 长 4 个字节,但是表示的数值范围却是一样的。
long double 类型的长度、精度及表示范围与所使用的编译器、操作系统等有关。 -
各种数据类型的取值范围(自查)
2012-11-21 16:13:32这时候就会出现一些困惑各种数据类型的大小,声明的时候让自己很谨慎, 于是碰到较大的数值就直接long类型的的变量就声明了,在C函数库Limits中包含着对于所用编译器中各个数据类型的取值范围。下面是程序代码。 /*...在写程序的过程中我们有时候会处理一些极大地数据,这时候就会出现一些困惑各种数据类型的大小,声明的时候让自己很谨慎, 于是碰到较大的数值就直接long类型的的变量就声明了,在C函数库Limits中包含着对于所用编译器中各个数据类型的取值范围。下面是程序代码。
/*本程序中得到启示:在输出不同类型的数据时,输出格式也要相应改变 * long类型的要添加‘l’,unsigned要添加'u'否则会发生越界*/ #include <limits.h> #include <stdio.h> int main() { printf("The Bits Of Type Char: %d \n",CHAR_BIT); printf("The Max Of Char: %d \n",CHAR_MAX); printf("The Min Of Char: %d \n",CHAR_MIN); printf("The Max Of Int: %d \n",INT_MAX); printf("The Min Of Int: %d \n",INT_MIN); printf("The Max Of Long: %ld \n",LONG_MAX); printf("The Min Of Long: %ld \n",LONG_MIN); printf("The Max Of Short: %d \n",SHRT_MAX); printf("The Min Of Short: %d \n",SHRT_MIN); printf("The Max Of unsigned Char: %u \n",UCHAR_MAX); //此处如果是%d,会显示越界 printf("The Max Of unsigned Int: %u \n",UINT_MAX); printf("The Max Of unsigned Short: %u \n",USHRT_MAX); //此处如果是%d,会显示越界 printf("The Max Of unsigned Long: %lu \n",ULONG_MAX); return 0; }
这是我自己的电脑windows32位机的运行结果。
通过上图可以发现int与long的数值范围是一致的,因为在32位操作系统中,int跟long都占4字节,char占1个字节,short占两个字节。数值最大取值为1000000000(<2147483647),在32位系统中时完全不会越界的。(平时都被一些极为老式的教科书上说的int占2个字节声明为unsigned最大才为65535给蒙了)。
-
各种数据类型的取值范围(总结全)
2016-07-18 10:41:13sbyte型为有符号8位整数,占1个字节,取值范围在128~127之间。 bytet型为无符号16位整数,占2个字节,取值范围在0~255之间。 short型为有符号16位整数,占2个字节,取值范围在-32,768~32,767之间。 ushort型为无符号已测试;
32位下:
bool 1个字节 0/1。 BOOL 4个字节 TRUE/FALSE/ERROR。 char 1个字节 -128~127 unsigned char 1个字节 0~255 short 2个字节 -32768~32767 unsigned short 2个字节 0~65535 int 4个字节 -2147483648~2147483647 unsigned int 4个字节 0~4294967295 long 4个字节 -2147483648~2147483647 unsigned long 4个字节 0~4294967295 long long 8个字节 -9223372036854775808~9223372036854775807 unsigned long long 8个字节 0~18446744073709551615
指针 4个字节
浮点数:
类型
位数(字节数)
有效数字
数值范围
float
32(4)
6~7
-3.4*10^38~+3.4*10^38
double
64(8)
15~16
-1.7*10^-308~1.7*10^308
long double
96(12)
18~19
-1.2*10^-4932~1.2*10^4932
关于double的超大取值范围:因为double类型是浮点数,这种类型可以用科学记数法表示,所以表示范围非常大。但是,使用可浮点数的代价就是损失了精度。它把这部分精度用于指数的表示。所以double类型的优点就是数据范围大,缺点是精度不足,大概只有15~16位有效位数。注意:占多少个字节是由编译器决定的,ANSI标准定义int是占2个字节.TC是按ANSI标准的,它的int是占2个字节的.你可以在TC里试.printf("%d",sizeof(int));结果是2;但是在VC里,一个int是占4个字节的,在VC里面,printf("%d",sizeof(int));cout<<sizeof(int);结果都是4.不同的编译器,规定也不一样.float,double也是一样的,在不同的编译器里,占的字节是不一样的 -
(整理)c++中找出各种数据类型的取值范围
2018-02-02 21:45:14今天去编程之美打酱油了,又遇到了数据类型取值范围的问题。下面整理一下网上其他地方的资料。 整理自: http://blog.csdn.net/xuexiacm/article/details/8122267 和 http://blog.csdn.net/ying转自https://www.cnblogs.com/cquljw/p/3662821.html
今天去编程之美打酱油了,又遇到了数据类型取值范围的问题。下面整理一下网上其他地方的资料。
整理自:
http://blog.csdn.net/xuexiacm/article/details/8122267
和
http://blog.csdn.net/yingevil/article/details/6690863
如下:
#include<iostream> #include<string> #include <limits> using namespace std; int main() { cout << "type: \t\t" << "************size**************" << endl; cout << "bool: \t\t" << "所占字节数:" << sizeof(bool); cout << "\t最大值:" << (numeric_limits<bool>::max)(); cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl; cout << "char: \t\t" << "所占字节数:" << sizeof(char); cout << "\t最大值:" << (numeric_limits<char>::max)(); cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl; cout << "signed char: \t" << "所占字节数:" << sizeof(signed char); cout << "\t最大值:" << (numeric_limits<signed char>::max)(); cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl; cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char); cout << "\t最大值:" << (numeric_limits<unsigned char>::max)(); cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl; cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t); cout << "\t最大值:" << (numeric_limits<wchar_t>::max)(); cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl; cout << "short: \t\t" << "所占字节数:" << sizeof(short); cout << "\t最大值:" << (numeric_limits<short>::max)(); cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl; cout << "int: \t\t" << "所占字节数:" << sizeof(int); cout << "\t最大值:" << (numeric_limits<int>::max)(); cout << "\t最小值:" << (numeric_limits<int>::min)() << endl; cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned); cout << "\t最大值:" << (numeric_limits<unsigned>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl; cout << "long: \t\t" << "所占字节数:" << sizeof(long); cout << "\t最大值:" << (numeric_limits<long>::max)(); cout << "\t最小值:" << (numeric_limits<long>::min)() << endl; cout << "long long: \t\t" << "所占字节数:" << sizeof(long long); cout << "\t最大值:" << (numeric_limits<long long>::max)(); cout << "\t最小值:" << (numeric_limits<long long>::min)() << endl; cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long); cout << "\t最大值:" << (numeric_limits<unsigned long>::max)(); cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl; cout << "double: \t" << "所占字节数:" << sizeof(double); cout << "\t最大值:" << (numeric_limits<double>::max)(); cout << "\t最小值:" << (numeric_limits<double>::min)() << endl; cout << "long double: \t" << "所占字节数:" << sizeof(long double); cout << "\t最大值:" << (numeric_limits<long double>::max)(); cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl; cout << "float: \t\t" << "所占字节数:" << sizeof(float); cout << "\t最大值:" << (numeric_limits<float>::max)(); cout << "\t最小值:" << (numeric_limits<float>::min)() << endl; cout << "size_t: \t" << "所占字节数:" << sizeof(size_t); cout << "\t最大值:" << (numeric_limits<size_t>::max)(); cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl; cout << "string: \t" << "所占字节数:" << sizeof(string) << endl; // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl; cout << "type: \t\t" << "************size**************" << endl; return 0; }
代码是参考这个的:
http://blog.csdn.net/xuexiacm/article/details/8122267
我这边加入了long long类型的测试后运行结果是:
/*运行结果分析:
以上结果已经很明白了,一下补充说明几点:
概念、整型:表示整数、字符和布尔值的算术类型合称为整型(integral type)。
关于带符号与无符号类型:整型 int、stort 和 long 都默认为带符号型。要获得无符号型则必须制定该类型为unsigned,比如unsigned long。unsigned int类型可以简写为unsigned,也就是说,unsigned后不加其他类型说明符就意味着是unsigned int。
一字节表示八位,即:1byte = 8 bit;
int: 4byte = 32 bit有符号signed范围:2^31-1 ~ -2^31即:2147483647 ~ -2147483648无符号unsigned范围:2^32-1 ~ 0即:4294967295 ~ 0
long: 4 byte = 32 bit同int型
double: 8 byte = 64 bit范围:1.79769e+308 ~ 2.22507e-308
long double: 12 byte = 96 bit范围: 1.18973e+4932 ~ 3.3621e-4932
float: 4 byte = 32 bit范围: 3.40282e+038 ~ 1.17549e-038
int、unsigned、long、unsigned long 、double的数量级最大都只能表示为10亿,即它们表示十进制的位数不超过10个,即可以保存所有9位整数。而short只是能表示5位;
另外对于浮点说而言:使用double类型基本上不会有错。在float类型中隐式的精度损失是不能忽视的,二双精度计算的代价相对于单精度可以忽略。事实上,在有些机器上,double类型比float类型的计算要快得多。float型只能保证6位有效数字,而double型至少可以保证15位有效数字(小数点后的数位),long double型提供的精度通常没有必要,而且还要承担额外的运行代价。
double是8字节共64位,其中小数位占52位,2-^52=2.2204460492503130808472633361816e-16,量级为10^-16,故能够保证2^-15的所有精度。
在有些机器上,用long类型进行计算所付出的运行时代价远远高于用int类型进行同样计算的代价,所以算则类型前要先了解程序的细节并且比较long类型与int类型的实际运行时性能代价。
然后是另一篇关于__int64和long long的文章:
http://blog.csdn.net/yingevil/article/details/6690863
也转到这里:
C语言中long long的用法
在C语言的C99标准扩展了新的整数类型 long long,long是32位宽,占4个字节,long long通常被定义成 64 位宽,也就可以实现了在32位机器上可以扩展8字节的数据,GUN C也支持,当然在64位平台上就存在这个问题了。C99标准并没有硬性规定具体到某种平台上的某种整数类型究竟占用多少字节、能够表示多大范围的数值等,只是给出一条原则和一个参考数值集合,只要同时满足这两方面条件就算是符合 C 标准。 之后,我查看了C99标准: —The rank of long long int shall be greater than the rank of long int,whichshall be greater than the rank of int,which shall be greater than the rank of short int,which shall be greater than the rank of signed char.
意思是说: long long 的级别高于 long ,long 的级别高于 int ,int 的级别高于 short ,short 的级别高于 char 。(另外有 _Bool 永远是最低级别)。级别高的整数类型的宽度大于等于级别较低的整数类型。
编译long long需要支持C99标准的编译器才行,VC并不支持,但有对应的类型__int64
C++ __int64用法
在做ACM题时,经常都会遇到一些比较大的整数。而常用的内置整数类型常常显得太小了:其中long 和 int 范围是[-2^31,2^31),即-2147483648~2147483647。而unsigned范围是[0,2^32),即0~4294967295。也就是说,常规的32位整数只能够处理40亿以下的数。 那遇到比40亿要大的数怎么办呢?这时就要用到C++的64位扩展了。不同的编译器对64位整数的扩展有所不同。基于ACM的需要,下面仅介绍VC6.0与g++编译器的扩展。 VC的64位整数分别叫做__int64与unsigned __int64,其范围分别是[-2^63, 2^63)与[0,2^64),即-9223372036854775808~9223372036854775807与0~18446744073709551615(约1800亿亿)。对64位整数的运算与32位整数基本相同,都支持四则运算与位运算等。当进行64位与32位的混合运算时,32位整数会被隐式转换成64位整数。但是,VC的输入输出与__int64的兼容就不是很好了,如果你写下这样一段代码:
那么,在第2行会收到“error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '__int64' (or there is no acceptable conversion)”的错误;在第3行会收到“error C2593: 'operator <<' is ambiguous”的错误。那是不是就不能进行输入输出呢?当然不是,你可以使用C的写法:
scanf("%I64d",&a); printf("%I64d",a);
就可以正确输入输出了。当使用unsigned __int64时,把"I64d"改为"I64u"就可以了。 OJ通常使用g++编译器。其64位扩展方式与VC有所不同,它们分别叫做long long 与 unsigned long long。处理规模与除输入输出外的使用方法同上。对于输入输出,它的扩展比VC好。既可以使用
cin>>a; 3 cout<<a;
也可以使用
scanf("%lld",&a); printf("%lld",a);
最后我补充一点:作为一个特例,如果你使用的是Dev-C++的g++编译器,它使用的是"%I64d"而非"%lld"。总结:
输入输出long long 也可以借助printf,scanf语句,
但对应的占位符却是和平台相关与编译器相关的:
在Linux中,gcc很统一的用%lld;在windows中,MinGW的gcc和VC6都需要用%I64d;
但VS2008却是用%lld。
-
浅析各种数据类型的取值范围
2011-04-15 00:05:00每次在用的时候对数据类型范围很模糊,到底会不会溢出,该不该用long long之类的,这次找了相关资料给总结下 char1-128到126 unsignedchar10到255 short2-32,768到32,767 unsignedshort20到65,535... -
[转]C++各种数据类型的取值范围
2015-09-06 16:06:00先看一个例子,貌似有些编译器并不完全支持所有数据类型,运行结果可能也有差异,依编译器而异,但是获得方式都是一样的、、 #include<stdio.h> #include<limits.h> #include<float.h> #... -
【C/C++系列】各种数据类型的取值范围(常用)
2018-01-25 19:45:30转载自:http://blog.csdn.net/mafuli007/article/details/7325510 速查表: char -128 ~ +127 (1 Byte) short -32767 ~ + 32768 (2 Bytes) unsigned short 0 ~ 65536 (2 Bytes) int -2147483648 ~ +21474836 -
【转】各种数据类型的取值范围 C/C++标准库中定义的各个类型最大值最小值limit.h
2017-07-05 08:54:34转载自:... C/C++标准库中定义的各个类型最大值最小值limit.h 编程中,我们一般需要考虑各个数据类型能够存储的最大值。虽然我们知道例如char是1个字节,short int是2个字节 -
各种数据类型的取值范围[转]----C/C++
2013-12-26 13:19:50取值范围 输入符举例 输出符举例 -- -- char 8 -2^7 ~ 2^7-1 %c 、 %d 、 %u signed -- char 8 -2^7 ~ 2^7-1 %c 、 %d 、 %u unsigned -- char 8... -
编译器定义的C/C++语言各种基本数据类型的取值范围
2017-11-12 15:43:00貌似有些编译器并不完全支持所有数据类型,运行结果可能也有差异,依编译器而异,但是获得方式都是一样的 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include<stdio.h... -
mysql的float取值范围_MYSQL中支持的数据类型及取值范围
2021-01-18 19:30:04下表列出了各种数值类型以及它们的允许范围和占用的内存空间。类型大小范围(有符号)范围(无符号)用途TINYINT1字节-128~1270~255小整数值SMALLINT2字节-32768~327670~65535大整数值MEDIUMINT3字节-8388608~83886070~... -
SQL 用于各种数据库的数据类型(转载) sqlserver 数据类型 取值范围 长度
2018-10-19 11:45:00来源 http://www.runoob.com/sql/sql-datatypes.html 面向数据库编程中,数据类型的取值范围、长度,可能是需要经常查看的资料。 Microsoft Access、MySQL 和 SQL Server 所使用的数据类型和范围。 Microsoft...