What is the function to determine the min and max possible of value of datatypes (i.e, int, char.etc) in C?
确定数据类型(i)的最小和最大可能值的函数是什么?e、int、char.etc)在C?
9 个解决方案
#1
81
You'll want to use limits.h
which provides the following constants (as per the linked reference):
你需要使用极限。h,提供以下常数(按相关参考):
CHAR_BIT = number of bits in a char
SCHAR_MIN = minimum value for a signed char
SCHAR_MAX = maximum value for a signed char
UCHAR_MAX = maximum value for an unsigned char
CHAR_MIN = minimum value for a char
CHAR_MAX = maximum value for a char
MB_LEN_MAX = maximum multibyte length of a character accross locales
SHRT_MIN = minimum value for a short
SHRT_MAX = maximum value for a short
USHRT_MAX = maximum value for an unsigned short
INT_MIN = minimum value for an int
INT_MAX = maximum value for an int
UINT_MAX = maximum value for an unsigned int
LONG_MIN = minimum value for a long
LONG_MAX = maximum value for a long
ULONG_MAX = maximum value for an unsigned long
LLONG_MIN = minimum value for a long long
LLONG_MAX = maximum value for a long long
ULLONG_MAX = maximum value for an unsigned long long
Where U*_MIN
is omitted for obvious reasons (any unsigned type has a minimum value of 0).
由于明显的原因,省略了U*_MIN(任何未签名类型的最小值为0)。
Similarly float.h
provides limits for float
and double
types:
类似的浮动。h提供浮动和双类型的限制:
-FLT_MAX = most negative value of a float
FLT_MAX = max value of a float
-DBL_MAX = most negative value of a double
DBL_MAX = max value of a double
-LDBL_MAX = most negative value of a long double
LDBL_MAX = max value of a long double
You should read the article on floats.h
carefully, though float
and double
can hold the prescribed minimum and maximum values but the precision with which each type can represent data may not match what it is you're trying to store. In particular, it's difficult to store exceptionally large numbers with extremely small fractions attached. So float.h
provides a number of other constants that help you to determine if a float
or a double
can,in fact,represent a particular number.
您应该阅读关于浮动的文章。注意,虽然float和double可以保存指定的最小值和最大值,但是每种类型表示数据的精度可能与要存储的数据不匹配。特别地,很难用极小的分数来存储异常大的数。所以浮动。h提供了许多其他常量,可以帮助您确定一个浮点数或双精度浮点数实际上是否代表一个特定的数字。
#2
25
"But glyph", I hear you asking, "what if I have to determine the maximum value for an opaque type whose maximum might eventually change?" You might continue: "What if it's a typedef in a library I don't control?"
“但是字形”,我听到你在问,“如果我必须确定一个不透明类型的最大值,它的最大值可能最终会改变呢?”您可能会继续:“如果它是一个我无法控制的库中的类型定义呢?”
I'm glad you asked, because I just spent a couple of hours cooking up a solution (which I then had to throw away, because it didn't solve my actual problem).
我很高兴你问我这个问题,因为我刚刚花了几个小时想出一个解决方案(后来我不得不放弃了这个方案,因为它并不能解决我真正的问题)。
You can use this handy maxof
macro to determine the size of any valid integer type.
您可以使用这个方便的maxof宏来确定任何有效整数类型的大小。
#define issigned(t) (((t)(-1)) < ((t) 0))
#define umaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define smaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define maxof(t) ((unsigned long long) (issigned(t) ? smaxof(t) : umaxof(t)))
You can use it like so:
你可以这样使用它:
int main(int argc, char** argv) {
printf("schar: %llx uchar: %llx\n", maxof(char), maxof(unsigned char));
printf("sshort: %llx ushort: %llx\n", maxof(short), maxof(unsigned short));
printf("sint: %llx uint: %llx\n", maxof(int), maxof(unsigned int));
printf("slong: %llx ulong: %llx\n", maxof(long), maxof(unsigned long));
printf("slong long: %llx ulong long: %llx\n",
maxof(long long), maxof(unsigned long long));
return 0;
}
If you'd like, you can toss a '(t)' onto the front of those macros so they give you a result of the type that you're asking about, and you don't have to do casting to avoid warnings.
如果你愿意,你可以在这些宏的前面加上一个'(t)',这样它们就会给出你要问的类型的结果,你不需要通过强制转换来避免警告。
#3
5
Maximum value of any unsigned integral type: (~(t)0)
任何无符号整型的最大值:(~(t)0)
Maximum value of any signed integral type: If you have an unsigned variant of type t, ((t)((~(unsigned t)0)>>1))
will give you the fastest result you need (see example in Linux kernel source code referenced below). Otherwise, you should probably use (~(1ULL<<(sizeof(t)*CHAR_BIT-1)))
.
任何有符号整型的最大值:如果你有一个没有符号的t型变异体,(t)((((~(无符号t)0)>>1)将会给出你需要的最快结果(参见下面引用的Linux内核源代码示例)。否则,您应该使用(~(1ULL<<(sizeof(t)*CHAR_BIT-1))。
Minimum value of any signed integral type: You have to know the signed number representation of your machine. Most machines use 2's complement, and so -(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1
will work for you.
任何符号整数类型的最小值:你必须知道你的机器的签名数字表示。大多数机器都使用2的补码,所以-(~(1ULL< (sizeof(t)*CHAR_BIT-1)) -1将对您有效。
To detect whether your machine uses 2's complement, detect whether (~(t)0U)
and (t)(-1)
represent the same thing. So, combined with above:
要检测您的机器是否使用2的补码,请检测(~(t)0U)和(t)(-1)是否表示相同的东西。因此,结合上图:
((~(t)0U) == (t)(-1) ? -(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1 :
-(~(1ULL<<(sizeof(t)*CHAR_BIT-1))))
will give you the minimum value of any signed integral type. (Actually there are other representations of this if you know 2's complement representation. For example (t)(1ULL<<(sizeof(t)*CHAR_BIT-1))
should be equivalent to (t)(-(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1)
.)
将给出任何符号整数类型的最小值。如果你知道2的补码表示法,其实还有其他的形式。例如(t)(1妳< <(sizeof(t)* CHAR_BIT-1))应该相当于(t)(-(~(1妳< <(sizeof(t)* CHAR_BIT-1)))1)。)
For example: (~(size_t)0) gives you the maximum value of size_t. (And guess what, this is how SIZE_MAX is #defined in Linux kernel source code.)
例如:(~(size_t)0)给出了size_t的最大值。(猜猜看,这就是在Linux内核源代码中,SIZE_MAX是如何定义#的。)
One caveat though: all of these expressions use type casting and so don't work in preprocessor conditionals (#if ... #elif ... #endif and like).
但有一点需要注意:所有这些表达式都使用类型转换,因此不能在预处理程序条件中使用。# elif……# endif像)。
#4
#5
3
I wrote some macros that return the min and max of any type, regardless of signedness:
我写了一些宏来返回任意类型的最小值和最大值,而不考虑符号:
#define MAX_OF(type) \
(((type)(~0LLU) > (type)((1LLU<<((sizeof(type)<<3)-1))-1LLU)) ? (long long unsigned int)(type)(~0LLU) : (long long unsigned int)(type)((1LLU<<((sizeof(type)<<3)-1))-1LLU))
#define MIN_OF(type) \
(((type)(1LLU<<((sizeof(type)<<3)-1)) < (type)1) ? (long long int)((~0LLU)-((1LLU<<((sizeof(type)<<3)-1))-1LLU)) : 0LL)
Example code:
示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <inttypes.h>
#define MAX_OF(type) \
(((type)(~0LLU) > (type)((1LLU<<((sizeof(type)<<3)-1))-1LLU)) ? (long long unsigned int)(type)(~0LLU) : (long long unsigned int)(type)((1LLU<<((sizeof(type)<<3)-1))-1LLU))
#define MIN_OF(type) \
(((type)(1LLU<<((sizeof(type)<<3)-1)) < (type)1) ? (long long int)((~0LLU)-((1LLU<<((sizeof(type)<<3)-1))-1LLU)) : 0LL)
int main(void)
{
printf("uint32_t = %lld..%llu\n", MIN_OF(uint32_t), MAX_OF(uint32_t));
printf("int32_t = %lld..%llu\n", MIN_OF(int32_t), MAX_OF(int32_t));
printf("uint64_t = %lld..%llu\n", MIN_OF(uint64_t), MAX_OF(uint64_t));
printf("int64_t = %lld..%llu\n", MIN_OF(int64_t), MAX_OF(int64_t));
printf("size_t = %lld..%llu\n", MIN_OF(size_t), MAX_OF(size_t));
printf("ssize_t = %lld..%llu\n", MIN_OF(ssize_t), MAX_OF(ssize_t));
printf("pid_t = %lld..%llu\n", MIN_OF(pid_t), MAX_OF(pid_t));
printf("time_t = %lld..%llu\n", MIN_OF(time_t), MAX_OF(time_t));
printf("intptr_t = %lld..%llu\n", MIN_OF(intptr_t), MAX_OF(intptr_t));
printf("unsigned char = %lld..%llu\n", MIN_OF(unsigned char), MAX_OF(unsigned char));
printf("char = %lld..%llu\n", MIN_OF(char), MAX_OF(char));
printf("uint8_t = %lld..%llu\n", MIN_OF(uint8_t), MAX_OF(uint8_t));
printf("int8_t = %lld..%llu\n", MIN_OF(int8_t), MAX_OF(int8_t));
printf("uint16_t = %lld..%llu\n", MIN_OF(uint16_t), MAX_OF(uint16_t));
printf("int16_t = %lld..%llu\n", MIN_OF(int16_t), MAX_OF(int16_t));
printf("int = %lld..%llu\n", MIN_OF(int), MAX_OF(int));
printf("long int = %lld..%llu\n", MIN_OF(long int), MAX_OF(long int));
printf("long long int = %lld..%llu\n", MIN_OF(long long int), MAX_OF(long long int));
printf("off_t = %lld..%llu\n", MIN_OF(off_t), MAX_OF(off_t));
return 0;
}
#6
3
The header file limits.h
defines macros that expand to various limits and parameters of the standard integer types.
头文件的限制。h定义扩展到标准整数类型的各种限制和参数的宏。
#7
3
#include<stdio.h>
int main(void)
{
printf("Minimum Signed Char %d\n",-(char)((unsigned char) ~0 >> 1) - 1);
printf("Maximum Signed Char %d\n",(char) ((unsigned char) ~0 >> 1));
printf("Minimum Signed Short %d\n",-(short)((unsigned short)~0 >>1) -1);
printf("Maximum Signed Short %d\n",(short)((unsigned short)~0 >> 1));
printf("Minimum Signed Int %d\n",-(int)((unsigned int)~0 >> 1) -1);
printf("Maximum Signed Int %d\n",(int)((unsigned int)~0 >> 1));
printf("Minimum Signed Long %ld\n",-(long)((unsigned long)~0 >>1) -1);
printf("Maximum signed Long %ld\n",(long)((unsigned long)~0 >> 1));
/* Unsigned Maximum Values */
printf("Maximum Unsigned Char %d\n",(unsigned char)~0);
printf("Maximum Unsigned Short %d\n",(unsigned short)~0);
printf("Maximum Unsigned Int %u\n",(unsigned int)~0);
printf("Maximum Unsigned Long %lu\n",(unsigned long)~0);
return 0;
}
#8
0
MIN and MAX values of any integer data type can be computed without using any library functions as below and same logic can be applied to other integer types short, int and long.
任何整数数据类型的最小值和最大值都可以在不使用任何库函数的情况下进行计算,同样的逻辑也可以应用于其他整数类型,短、int和长。
printf("Signed Char : MIN -> %d & Max -> %d\n", ~(char)((unsigned char)~0>>1), (char)((unsigned char)~0 >> 1));
printf("Unsigned Char : MIN -> %u & Max -> %u\n", (unsigned char)0, (unsigned char)(~0));
#9
0
To get the maximum value of an unsigned integer type t
whose width is at least the one of unsigned int
(otherwise one gets problems with integer promotions): ~(t) 0
. If one wants to also support shorter types, one can add another cast: (t) ~(t) 0
.
要获得无符号整数类型t的最大值,其宽度至少为无符号整数类型int的最大值(否则会遇到整数提升的问题):~(t) 0。如果想支持更短的类型,可以添加另一个cast: (t) ~(t) 0。
If the integer type t
is signed, assuming that there are no padding bits, one can use:
如果整数类型t是有符号的,假设没有填充位,可以使用:
((((t) 1 << (sizeof(t) * CHAR_BIT - 2)) - 1) * 2 + 1)
The advantage of this formula is that it is not based on some unsigned version of t
(or a larger type), which may be unknown or unavailable (even uintmax_t
may not be sufficient with non-standard extensions). Example with 6 bits (not possible in practice, just for readability):
这个公式的优点是它不是基于某个未签名的t(或较大的类型),它可能是未知的或不可用的(甚至uintmax_t对于非标准扩展来说也可能是不够的)。以6位为例(在实践中是不可能的,只是为了便于阅读):
010000 (t) 1 << (sizeof(t) * CHAR_BIT - 2)
001111 - 1
011110 * 2
011111 + 1
In two's complement, the minimum value is the opposite of the maximum value, minus 1 (in the other integer representations allowed by the ISO C standard, this is just the opposite of the maximum value).
在2的补码中,最小值与最大值- 1正好相反(在ISO C标准允许的其他整数表示中,这个值正好与最大值相反)。
Note: To detect signedness in order to decide which version to use: (t) -1 < 0
will work with any integer representation, giving 1 (true) for signed integer types and 0 (false) for unsigned integer types. Thus one can use:
注意:为了检测签字率,以决定使用哪个版本:(t) -1 < 0将使用任何整数表示,有符号整数类型为1 (true),无符号整数类型为0 (false)。因此一个可以使用:
(t) -1 < 0 ? ((((t) 1 << (sizeof(t) * CHAR_BIT - 2)) - 1) * 2 + 1) : (t) ~(t) 0
#1
81
You'll want to use limits.h
which provides the following constants (as per the linked reference):
你需要使用极限。h,提供以下常数(按相关参考):
CHAR_BIT = number of bits in a char
SCHAR_MIN = minimum value for a signed char
SCHAR_MAX = maximum value for a signed char
UCHAR_MAX = maximum value for an unsigned char
CHAR_MIN = minimum value for a char
CHAR_MAX = maximum value for a char
MB_LEN_MAX = maximum multibyte length of a character accross locales
SHRT_MIN = minimum value for a short
SHRT_MAX = maximum value for a short
USHRT_MAX = maximum value for an unsigned short
INT_MIN = minimum value for an int
INT_MAX = maximum value for an int
UINT_MAX = maximum value for an unsigned int
LONG_MIN = minimum value for a long
LONG_MAX = maximum value for a long
ULONG_MAX = maximum value for an unsigned long
LLONG_MIN = minimum value for a long long
LLONG_MAX = maximum value for a long long
ULLONG_MAX = maximum value for an unsigned long long
Where U*_MIN
is omitted for obvious reasons (any unsigned type has a minimum value of 0).
由于明显的原因,省略了U*_MIN(任何未签名类型的最小值为0)。
Similarly float.h
provides limits for float
and double
types:
类似的浮动。h提供浮动和双类型的限制:
-FLT_MAX = most negative value of a float
FLT_MAX = max value of a float
-DBL_MAX = most negative value of a double
DBL_MAX = max value of a double
-LDBL_MAX = most negative value of a long double
LDBL_MAX = max value of a long double
You should read the article on floats.h
carefully, though float
and double
can hold the prescribed minimum and maximum values but the precision with which each type can represent data may not match what it is you're trying to store. In particular, it's difficult to store exceptionally large numbers with extremely small fractions attached. So float.h
provides a number of other constants that help you to determine if a float
or a double
can,in fact,represent a particular number.
您应该阅读关于浮动的文章。注意,虽然float和double可以保存指定的最小值和最大值,但是每种类型表示数据的精度可能与要存储的数据不匹配。特别地,很难用极小的分数来存储异常大的数。所以浮动。h提供了许多其他常量,可以帮助您确定一个浮点数或双精度浮点数实际上是否代表一个特定的数字。
#2
25
"But glyph", I hear you asking, "what if I have to determine the maximum value for an opaque type whose maximum might eventually change?" You might continue: "What if it's a typedef in a library I don't control?"
“但是字形”,我听到你在问,“如果我必须确定一个不透明类型的最大值,它的最大值可能最终会改变呢?”您可能会继续:“如果它是一个我无法控制的库中的类型定义呢?”
I'm glad you asked, because I just spent a couple of hours cooking up a solution (which I then had to throw away, because it didn't solve my actual problem).
我很高兴你问我这个问题,因为我刚刚花了几个小时想出一个解决方案(后来我不得不放弃了这个方案,因为它并不能解决我真正的问题)。
You can use this handy maxof
macro to determine the size of any valid integer type.
您可以使用这个方便的maxof宏来确定任何有效整数类型的大小。
#define issigned(t) (((t)(-1)) < ((t) 0))
#define umaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define smaxof(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | \
(0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
#define maxof(t) ((unsigned long long) (issigned(t) ? smaxof(t) : umaxof(t)))
You can use it like so:
你可以这样使用它:
int main(int argc, char** argv) {
printf("schar: %llx uchar: %llx\n", maxof(char), maxof(unsigned char));
printf("sshort: %llx ushort: %llx\n", maxof(short), maxof(unsigned short));
printf("sint: %llx uint: %llx\n", maxof(int), maxof(unsigned int));
printf("slong: %llx ulong: %llx\n", maxof(long), maxof(unsigned long));
printf("slong long: %llx ulong long: %llx\n",
maxof(long long), maxof(unsigned long long));
return 0;
}
If you'd like, you can toss a '(t)' onto the front of those macros so they give you a result of the type that you're asking about, and you don't have to do casting to avoid warnings.
如果你愿意,你可以在这些宏的前面加上一个'(t)',这样它们就会给出你要问的类型的结果,你不需要通过强制转换来避免警告。
#3
5
Maximum value of any unsigned integral type: (~(t)0)
任何无符号整型的最大值:(~(t)0)
Maximum value of any signed integral type: If you have an unsigned variant of type t, ((t)((~(unsigned t)0)>>1))
will give you the fastest result you need (see example in Linux kernel source code referenced below). Otherwise, you should probably use (~(1ULL<<(sizeof(t)*CHAR_BIT-1)))
.
任何有符号整型的最大值:如果你有一个没有符号的t型变异体,(t)((((~(无符号t)0)>>1)将会给出你需要的最快结果(参见下面引用的Linux内核源代码示例)。否则,您应该使用(~(1ULL<<(sizeof(t)*CHAR_BIT-1))。
Minimum value of any signed integral type: You have to know the signed number representation of your machine. Most machines use 2's complement, and so -(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1
will work for you.
任何符号整数类型的最小值:你必须知道你的机器的签名数字表示。大多数机器都使用2的补码,所以-(~(1ULL< (sizeof(t)*CHAR_BIT-1)) -1将对您有效。
To detect whether your machine uses 2's complement, detect whether (~(t)0U)
and (t)(-1)
represent the same thing. So, combined with above:
要检测您的机器是否使用2的补码,请检测(~(t)0U)和(t)(-1)是否表示相同的东西。因此,结合上图:
((~(t)0U) == (t)(-1) ? -(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1 :
-(~(1ULL<<(sizeof(t)*CHAR_BIT-1))))
will give you the minimum value of any signed integral type. (Actually there are other representations of this if you know 2's complement representation. For example (t)(1ULL<<(sizeof(t)*CHAR_BIT-1))
should be equivalent to (t)(-(~(1ULL<<(sizeof(t)*CHAR_BIT-1)))-1)
.)
将给出任何符号整数类型的最小值。如果你知道2的补码表示法,其实还有其他的形式。例如(t)(1妳< <(sizeof(t)* CHAR_BIT-1))应该相当于(t)(-(~(1妳< <(sizeof(t)* CHAR_BIT-1)))1)。)
For example: (~(size_t)0) gives you the maximum value of size_t. (And guess what, this is how SIZE_MAX is #defined in Linux kernel source code.)
例如:(~(size_t)0)给出了size_t的最大值。(猜猜看,这就是在Linux内核源代码中,SIZE_MAX是如何定义#的。)
One caveat though: all of these expressions use type casting and so don't work in preprocessor conditionals (#if ... #elif ... #endif and like).
但有一点需要注意:所有这些表达式都使用类型转换,因此不能在预处理程序条件中使用。# elif……# endif像)。
#4
3
Look at the these pages on limits.h and float.h, which are included as part of the standard c library.
看看这些限制页面。h和浮动。h,作为标准c库的一部分。
#5
3
I wrote some macros that return the min and max of any type, regardless of signedness:
我写了一些宏来返回任意类型的最小值和最大值,而不考虑符号:
#define MAX_OF(type) \
(((type)(~0LLU) > (type)((1LLU<<((sizeof(type)<<3)-1))-1LLU)) ? (long long unsigned int)(type)(~0LLU) : (long long unsigned int)(type)((1LLU<<((sizeof(type)<<3)-1))-1LLU))
#define MIN_OF(type) \
(((type)(1LLU<<((sizeof(type)<<3)-1)) < (type)1) ? (long long int)((~0LLU)-((1LLU<<((sizeof(type)<<3)-1))-1LLU)) : 0LL)
Example code:
示例代码:
#include <stdio.h>
#include <sys/types.h>
#include <inttypes.h>
#define MAX_OF(type) \
(((type)(~0LLU) > (type)((1LLU<<((sizeof(type)<<3)-1))-1LLU)) ? (long long unsigned int)(type)(~0LLU) : (long long unsigned int)(type)((1LLU<<((sizeof(type)<<3)-1))-1LLU))
#define MIN_OF(type) \
(((type)(1LLU<<((sizeof(type)<<3)-1)) < (type)1) ? (long long int)((~0LLU)-((1LLU<<((sizeof(type)<<3)-1))-1LLU)) : 0LL)
int main(void)
{
printf("uint32_t = %lld..%llu\n", MIN_OF(uint32_t), MAX_OF(uint32_t));
printf("int32_t = %lld..%llu\n", MIN_OF(int32_t), MAX_OF(int32_t));
printf("uint64_t = %lld..%llu\n", MIN_OF(uint64_t), MAX_OF(uint64_t));
printf("int64_t = %lld..%llu\n", MIN_OF(int64_t), MAX_OF(int64_t));
printf("size_t = %lld..%llu\n", MIN_OF(size_t), MAX_OF(size_t));
printf("ssize_t = %lld..%llu\n", MIN_OF(ssize_t), MAX_OF(ssize_t));
printf("pid_t = %lld..%llu\n", MIN_OF(pid_t), MAX_OF(pid_t));
printf("time_t = %lld..%llu\n", MIN_OF(time_t), MAX_OF(time_t));
printf("intptr_t = %lld..%llu\n", MIN_OF(intptr_t), MAX_OF(intptr_t));
printf("unsigned char = %lld..%llu\n", MIN_OF(unsigned char), MAX_OF(unsigned char));
printf("char = %lld..%llu\n", MIN_OF(char), MAX_OF(char));
printf("uint8_t = %lld..%llu\n", MIN_OF(uint8_t), MAX_OF(uint8_t));
printf("int8_t = %lld..%llu\n", MIN_OF(int8_t), MAX_OF(int8_t));
printf("uint16_t = %lld..%llu\n", MIN_OF(uint16_t), MAX_OF(uint16_t));
printf("int16_t = %lld..%llu\n", MIN_OF(int16_t), MAX_OF(int16_t));
printf("int = %lld..%llu\n", MIN_OF(int), MAX_OF(int));
printf("long int = %lld..%llu\n", MIN_OF(long int), MAX_OF(long int));
printf("long long int = %lld..%llu\n", MIN_OF(long long int), MAX_OF(long long int));
printf("off_t = %lld..%llu\n", MIN_OF(off_t), MAX_OF(off_t));
return 0;
}
#6
3
The header file limits.h
defines macros that expand to various limits and parameters of the standard integer types.
头文件的限制。h定义扩展到标准整数类型的各种限制和参数的宏。
#7
3
#include<stdio.h>
int main(void)
{
printf("Minimum Signed Char %d\n",-(char)((unsigned char) ~0 >> 1) - 1);
printf("Maximum Signed Char %d\n",(char) ((unsigned char) ~0 >> 1));
printf("Minimum Signed Short %d\n",-(short)((unsigned short)~0 >>1) -1);
printf("Maximum Signed Short %d\n",(short)((unsigned short)~0 >> 1));
printf("Minimum Signed Int %d\n",-(int)((unsigned int)~0 >> 1) -1);
printf("Maximum Signed Int %d\n",(int)((unsigned int)~0 >> 1));
printf("Minimum Signed Long %ld\n",-(long)((unsigned long)~0 >>1) -1);
printf("Maximum signed Long %ld\n",(long)((unsigned long)~0 >> 1));
/* Unsigned Maximum Values */
printf("Maximum Unsigned Char %d\n",(unsigned char)~0);
printf("Maximum Unsigned Short %d\n",(unsigned short)~0);
printf("Maximum Unsigned Int %u\n",(unsigned int)~0);
printf("Maximum Unsigned Long %lu\n",(unsigned long)~0);
return 0;
}
#8
0
MIN and MAX values of any integer data type can be computed without using any library functions as below and same logic can be applied to other integer types short, int and long.
任何整数数据类型的最小值和最大值都可以在不使用任何库函数的情况下进行计算,同样的逻辑也可以应用于其他整数类型,短、int和长。
printf("Signed Char : MIN -> %d & Max -> %d\n", ~(char)((unsigned char)~0>>1), (char)((unsigned char)~0 >> 1));
printf("Unsigned Char : MIN -> %u & Max -> %u\n", (unsigned char)0, (unsigned char)(~0));
#9
0
To get the maximum value of an unsigned integer type t
whose width is at least the one of unsigned int
(otherwise one gets problems with integer promotions): ~(t) 0
. If one wants to also support shorter types, one can add another cast: (t) ~(t) 0
.
要获得无符号整数类型t的最大值,其宽度至少为无符号整数类型int的最大值(否则会遇到整数提升的问题):~(t) 0。如果想支持更短的类型,可以添加另一个cast: (t) ~(t) 0。
If the integer type t
is signed, assuming that there are no padding bits, one can use:
如果整数类型t是有符号的,假设没有填充位,可以使用:
((((t) 1 << (sizeof(t) * CHAR_BIT - 2)) - 1) * 2 + 1)
The advantage of this formula is that it is not based on some unsigned version of t
(or a larger type), which may be unknown or unavailable (even uintmax_t
may not be sufficient with non-standard extensions). Example with 6 bits (not possible in practice, just for readability):
这个公式的优点是它不是基于某个未签名的t(或较大的类型),它可能是未知的或不可用的(甚至uintmax_t对于非标准扩展来说也可能是不够的)。以6位为例(在实践中是不可能的,只是为了便于阅读):
010000 (t) 1 << (sizeof(t) * CHAR_BIT - 2)
001111 - 1
011110 * 2
011111 + 1
In two's complement, the minimum value is the opposite of the maximum value, minus 1 (in the other integer representations allowed by the ISO C standard, this is just the opposite of the maximum value).
在2的补码中,最小值与最大值- 1正好相反(在ISO C标准允许的其他整数表示中,这个值正好与最大值相反)。
Note: To detect signedness in order to decide which version to use: (t) -1 < 0
will work with any integer representation, giving 1 (true) for signed integer types and 0 (false) for unsigned integer types. Thus one can use:
注意:为了检测签字率,以决定使用哪个版本:(t) -1 < 0将使用任何整数表示,有符号整数类型为1 (true),无符号整数类型为0 (false)。因此一个可以使用:
(t) -1 < 0 ? ((((t) 1 << (sizeof(t) * CHAR_BIT - 2)) - 1) * 2 + 1) : (t) ~(t) 0