在 C 语言中如何将变量设置为可能的最大数字?

发布于 2024-08-21 07:15:52 字数 36 浏览 4 评论 0原文

在 C 中如何将变量设置为无穷大(或任何保证的最大数值)?

How would you set a variable to equal infinity (or any guaranteed largest number value) in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

☆獨立☆ 2024-08-28 07:15:53

有一个名为 limit.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

There is a file called limits.h (at least on Linux there is), which holds this kind of definition e.g.

/* 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
东走西顾 2024-08-28 07:15:53

另一种获取整数最大值的可移植方法:

无符号整数

unsigned int uMax = (unsigned int)~0;

有符号整数

signed int iMax = (unsigned int)~0 >> 1;

解释

  • ~0 ->将所有位设置为 1
  • >> 1 ->擦除符号位,方法是将所有位向右移动一位
  • (unsigned int) 在位反转后将其类型转换为 unsigned int,而不是使用 ~0U,因为 C 不这样做有一个短字符后缀(一般情况下小于 int 的所有内容)

因此,为了获得最大可能的 char 值 - 只需将公式类型转换更改为
unsigned char 等。

额外的 - 有符号 int 的最小值

只需再次反转最大有符号 int 表达式中的所有位即可:

signed int iMin = ~((unsigned int)~0 >> 1);

将第一个符号位设置为 1,将其余位设置为 0

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)

So for biggest possible char value - just change in formula typecasting to
unsigned char and etc.

Bonus - minimum value of signed int

Simply just invert all bits once more in max signed int expression:

signed int iMin = ~((unsigned int)~0 >> 1);

That sets first sign bit to one and the rest bits - to zero

撧情箌佬 2024-08-28 07:15:53

到目前为止,获取无符号整数类型最大值的最简单方法是将 (-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.

与之呼应 2024-08-28 07:15:53

根据您的评论,您需要一个unsigned int(尽管您说“无符号整数”,所以也许您想要一个整数值,不一定是unsigned int)。

在 C 中,对于无符号整数类型,值 -1 在转换为该类型时,保证是该类型的最大值:

size_t size_max = -1;
unsigned int uint_max = -1;
unsigned long ulong_max = -1;

分配值 SIZE_MAX, UINT_MAXULONG_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 an 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:

size_t size_max = -1;
unsigned int uint_max = -1;
unsigned long ulong_max = -1;

assign the values SIZE_MAX, UINT_MAX and ULONG_MAX to the variables respectively. In general, you should include limits.h and use the appropriate macro, but it is nice to know the rule above. Also, SIZE_MAX is not in C89, so size_t size_max = -1; will work in C89 as well as C99.

Note that the overflow behavior is guaranteed only for unsigned integral types.

傲娇萝莉攻 2024-08-28 07:15:53

由于这个问题有一个 C++ 标签,我建议使用 numeric_limits:

#include <limits>

unsigned x = std::numeric_limits<unsigned>::max();

Since there's a C++ tag on this question, I'll suggest numeric_limits:

#include <limits>

unsigned x = std::numeric_limits<unsigned>::max();
勿挽旧人 2024-08-28 07:15:53

通常这是由 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 macro FP_INFINITE in math.h.

EDIT: Apparently Sam didn't actually want infinity, but integer limits, which can be found in limits.h like others have stated.

蓝眼泪 2024-08-28 07:15:53

我通常使用 limits.h INT_MAX 中的 *_MAX 宏来表示整数等。这些宏总是为变量类型正确设置。即使是明确大小的类型(例如 uint32)也会在此头文件中具有相应的条目。

它的优点是成为该类型的变量可以保存的最大可能值。

对于您在问题中要求的无符号整数,您可以使用 UINT_MAX

I generally use the *_MAX macros found in limits.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

悲凉≈ 2024-08-28 07:15:53

我想您可能想查看此链接:

http://www. gnu.org/s/libc/manual/html_node/Infinity-and-NaN.html

我这样做了,它在 gcc 4.4.1 上运行良好


#include "math.h"

int main(int argc, char**argv)
{ 
    int x = INFINITY; 
    return 0;
}

I guess you may want to check this link out:

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


#include "math.h"

int main(int argc, char**argv)
{ 
    int x = INFINITY; 
    return 0;
}

我们的影子 2024-08-28 07:15:53
  1. 首先,包含一个名为 math.h 的头文件。
  2. 现在,将 INT_MAX 等于要设置其最大值的整数。
    示例:#include//需要包含的头文件//
    int a=INT_MAX; //假设“a”是您希望其值最大的整数//
  1. Firstly, include a header file named as math.h
  2. 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//
瑕疵 2024-08-28 07:15:52
#include <limits.h>
int x = INT_MAX;

编辑:在提问者澄清之前回答,我只是猜测他们想要什么类型。

#include <limits.h>
int x = INT_MAX;

EDIT: answered before the questioner clarified, I was just guessing what type they wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文