在 C 语言中如何将变量设置为可能的最大数字?
在 C 中如何将变量设置为无穷大(或任何保证的最大数值)?
How would you set a variable to equal infinity (or any guaranteed largest number value) in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有一个名为 limit.h 的文件(至少在 Linux 上有),它包含这种定义,例如
There is a file called limits.h (at least on Linux there is), which holds this kind of definition e.g.
另一种获取整数最大值的可移植方法:
无符号整数
有符号整数
解释
~0
->将所有位设置为 1>> 1
->擦除符号位,方法是将所有位向右移动一位(unsigned int)
在位反转后将其类型转换为 unsigned int,而不是使用~0U
,因为 C 不这样做有一个短字符后缀(一般情况下小于 int 的所有内容)因此,为了获得最大可能的 char 值 - 只需将公式类型转换更改为
unsigned char 等。
额外的 - 有符号 int 的最小值
只需再次反转最大有符号 int 表达式中的所有位即可:
将第一个符号位设置为 1,将其余位设置为 0
Another portable way to get maximum value of integer:
Unsigned integer
Signed integer
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)So for biggest possible
char
value - just change in formula typecasting tounsigned char and etc.
Bonus - minimum value of signed int
Simply just invert all bits once more in max signed int expression:
That sets first sign bit to one and the rest bits - to zero
到目前为止,获取无符号整数类型最大值的最简单方法是将 (-1) 转换为该类型。标准 (§6.2.5/9) 要求无符号数学以比可表示的最大值大 1 的数为模进行计算,因此对于任何无符号类型
T
,表达式((T)-1)
必然是该类型中可能的最大值。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.根据您的评论,您需要一个
unsigned int
(尽管您说“无符号整数”,所以也许您想要一个整数值,不一定是unsigned int
)。在 C 中,对于无符号整数类型,值
-1
在转换为该类型时,保证是该类型的最大值:分配值
SIZE_MAX
,UINT_MAX
和ULONG_MAX
分别为变量。一般来说,您应该包含limits.h
并使用适当的宏,但最好了解上面的规则。另外,SIZE_MAX
不在 C89 中,因此size_t size_max = -1;
在 C89 和 C99 中都适用。请注意,仅对无符号整型类型保证溢出行为。
Based upon your comments, you want an
unsigned int
(although you say "unsigned integer", so maybe you want an integral value, not necessarily anunsigned int
).In C, for unsigned integral type, the value
-1
, when converted to that type, is guaranteed to be largest value of that type:assign the values
SIZE_MAX
,UINT_MAX
andULONG_MAX
to the variables respectively. In general, you should includelimits.h
and use the appropriate macro, but it is nice to know the rule above. Also,SIZE_MAX
is not in C89, sosize_t size_max = -1;
will work in C89 as well as C99.Note that the overflow behavior is guaranteed only for unsigned integral types.
由于这个问题有一个 C++ 标签,我建议使用 numeric_limits:
Since there's a C++ tag on this question, I'll suggest numeric_limits:
通常这是由
1.0/0.0
完成的,但您可能会收到编译警告。我不知道在 C89 中还有其他可移植的方法,但 C99 在math.h
中有宏FP_INFINITE
。编辑:显然 Sam 实际上并不想要无穷大,而是整数限制,可以像其他人所说的那样在
limits.h
中找到。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 macroFP_INFINITE
inmath.h
.EDIT: Apparently Sam didn't actually want infinity, but integer limits, which can be found in
limits.h
like others have stated.我通常使用
limits.h
INT_MAX
中的 *_MAX
宏来表示整数等。这些宏总是为变量类型正确设置。即使是明确大小的类型(例如 uint32)也会在此头文件中具有相应的条目。它的优点是成为该类型的变量可以保存的最大可能值。
对于您在问题中要求的无符号整数,您可以使用
UINT_MAX
I generally use the *
_MAX
macros found inlimits.h
INT_MAX
for 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.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
我想您可能想查看此链接:
我这样做了,它在 gcc 4.4.1 上运行良好
I guess you may want to check this link out:
I did this and it works fine on gcc 4.4.1
示例:
#include//需要包含的头文件//
int a=INT_MAX; //假设“a”是您希望其值最大的整数//
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//
编辑:在提问者澄清之前回答,我只是猜测他们想要什么类型。
EDIT: answered before the questioner clarified, I was just guessing what type they wanted.