C语言 如何将变量设置为 C 中可能的最大数字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2273913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would you set a variable to the largest number possible in C?
提问by Sam
How would you set a variable to equal infinity (or any guaranteed largest number value) in C?
如何在 C 中将变量设置为等于无穷大(或任何保证的最大数值)?
回答by dubiousjim
#include <limits.h>
int x = INT_MAX;
EDIT: answered before the questioner clarified, I was just guessing what type they wanted.
编辑:在提问者澄清之前回答,我只是在猜测他们想要什么类型。
回答by amo-ej1
There is a file called limits.h (at least on Linux there is), which holds this kind of definition e.g.
有一个名为limits.h 的文件(至少在Linux 上有),其中包含这种定义,例如
/* Maximum value an `unsigned short int' can hold. (Minimum is 0.) */
# define USHRT_MAX 65535
/* Minimum and maximum values a `signed int' can hold. */
# define INT_MIN (-INT_MAX - 1)
# define INT_MAX 2147483647
/* Maximum value an `unsigned int' can hold. (Minimum is 0.) */
# define UINT_MAX 4294967295U
回答by Jerry Coffin
By far the simplest method to get the largest value for an unsigned integer type is to cast (-1) to that type. The standard (§6.2.5/9) requires that unsigned math be carried out modulo a number one greater than the largest value that can be represented, so for any unsigned type T, the expression ((T)-1)will necessarily be the largest value possible in that type.
到目前为止,获得无符号整数类型最大值的最简单方法是将 (-1) 强制转换为该类型。标准(第 6.2.5/9 节)要求对无符号数学进行模数大于可以表示的最大值的一个数,因此对于任何无符号类型T,表达式((T)-1)必须是该类型中可能的最大值。
回答by Agnius Vasiliauskas
Another portable way to get maximum value of integer:
另一种获得整数最大值的便携方法:
Unsigned integer
无符号整数
unsigned int uMax = (unsigned int)~0;
Signed integer
有符号整数
signed int iMax = (unsigned int)~0 >> 1;
Explanation
解释
~0-> setting all bits to one>> 1-> erasing sign bit, by shifting all bits to the right by one position(unsigned int)typecasting to unsigned int after bits inversion instead of using~0U, because C doesn't have a suffix for short,char literals (everything smaller than int in general)
~0-> 将所有位设置为 1>> 1-> 擦除符号位,将所有位右移一位(unsigned int)在位反转后将类型转换为 unsigned int 而不是 using~0U,因为 C 没有 short,char 文字的后缀(通常所有小于 int 的)
So for biggest possible charvalue - just change in formula typecasting to
unsigned char and etc.
因此,对于最大可能的char价值 - 只需将公式类型转换更改为 unsigned char 等。
Bonus - minimum value of signed int
奖励 - 有符号整数的最小值
Simply just invert all bits once more in max signed int expression:
只需在最大有符号 int 表达式中再次反转所有位:
signed int iMin = ~((unsigned int)~0 >> 1);
That sets first sign bit to one and the rest bits - to zero
这将第一个符号位设置为一,其余位设置为零
回答by Alok Singhal
Based upon your comments, you want an unsigned int(although you say "unsigned integer", so maybe you want an integral value, not necessarily an unsigned int).
根据您的评论,您需要一个unsigned int(尽管您说的是“无符号整数”,因此您可能需要一个整数值,而不一定是unsigned int)。
In C, for unsigned integral type, the value -1, when converted to that type, is guaranteed to be largest value of that type:
在 C 中,对于无符号整数类型, value-1转换为该类型时,保证是该类型的最大值:
size_t size_max = -1;
unsigned int uint_max = -1;
unsigned long ulong_max = -1;
assign the values SIZE_MAX, UINT_MAXand ULONG_MAXto the variables respectively. In general, you should include limits.hand use the appropriate macro, but it is nice to know the rule above. Also, SIZE_MAXis not in C89, so size_t size_max = -1;will work in C89 as well as C99.
将值SIZE_MAX,UINT_MAX和ULONG_MAX分别分配给变量。通常,您应该包含limits.h并使用适当的宏,但了解上述规则会很好。此外,SIZE_MAX不在 C89 中,因此size_t size_max = -1;将在 C89 和 C99 中工作。
Note that the overflow behavior is guaranteed only for unsigned integral types.
请注意,溢出行为仅适用于无符号整数类型。
回答by Fred Larson
Since there's a C++ tag on this question, I'll suggest numeric_limits:
由于这个问题有一个 C++ 标签,我建议使用 numeric_limits:
#include <limits>
unsigned x = std::numeric_limits<unsigned>::max();
回答by Tronic
Normally this is done by 1.0/0.0, but you may get a compile warning on that. I am not aware of other portable ways of doing it in C89, but C99 has macro FP_INFINITEin math.h.
通常这是由 完成的1.0/0.0,但您可能会收到编译警告。我不知道在 C89 中还有其他可移植的方法,但是 C99FP_INFINITE在math.h.
EDIT: Apparently Sam didn't actually want infinity, but integer limits, which can be found in limits.hlike others have stated.
编辑:显然山姆实际上并不想要无穷大,而是整数限制,可以limits.h像其他人所说的那样找到。
回答by Andrew
I generally use the *_MAXmacros found in limits.hINT_MAXfor integers etc. These will always be correctly set for the variable type. Even the explicitly sized types such as uint32 will have corresponding entries in this header file.
我通常使用_MAX在limits.hINT_MAX整数等中找到的 *宏。这些将始终为变量类型正确设置。即使是显式大小的类型,如 uint32 也会在此头文件中具有相应的条目。
This has the virtue of being the largest possible value that a variable of that type can hold.
这具有成为该类型变量可以容纳的最大可能值的优点。
For an unsigned integer as you asked for in your question, you would use UINT_MAX
对于您在问题中要求的无符号整数,您可以使用 UINT_MAX
回答by thelinuxer
I guess you may want to check this link out:
我想您可能想查看此链接:
http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html
http://www.gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html
I did this and it works fine on gcc 4.4.1
我这样做了,它在 gcc 4.4.1 上运行良好
#include "math.h"
int main(int argc, char**argv)
{
int x = INFINITY;
return 0;
}
回答by sachin pathak
- Firstly, include a header file named as math.h
- Now, equate INT_MAX to the integer whose value you want to set maximum.
EXAMPLE:
#include<math.h> //the header file which need to be included// int a=INT_MAX; //Suppose "a" be that integer whose value you want largest//
- 首先,包含一个名为 math.h 的头文件
- 现在,将 INT_MAX 等同于要设置最大值的整数。例子:
#include<math.h> //the header file which need to be included// int a=INT_MAX; //Suppose "a" be that integer whose value you want largest//

