如何将一个变量设为C中最大的数?

时间:2022-09-06 14:39:25

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

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

9 个解决方案

#1


27  

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

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

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

#2


26  

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

有一个文件叫做极限。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

#3


9  

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)必然是最大的价值可能的类型。

#4


6  

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:

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

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.

分别为变量赋值SIZE_MAX、UINT_MAX和ULONG_MAX。一般来说,应该包括限制。使用适当的宏,但是很高兴知道上面的规则。同样,SIZE_MAX不在C89中,所以size_t SIZE_MAX = -1;将在C89和C99工作。

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

注意,溢出行为只对无符号整型进行保证。

#5


4  

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

由于这个问题有一个c++标记,我建议使用numeric_limit:

#include <limits>

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

#6


4  

Another portable way to get maximum value of unsigned integer - is to set all bits to one:

另一种获取无符号整数最大值的可移植方法是将所有位设置为1:

unsigned int uMax = ~0;

#7


2  

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.

通常这是在1.0/0.0处完成的,但是您可能会收到一个编译警告。我不知道在C89中还有其他可移植的方法,但是C99在math.h中有宏FP_INFINITE。

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

编辑:很明显Sam并不想要无穷大,而是要整数限制,可以在limit中找到。h就像其他人说的那样。

#8


1  

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.

我通常使用在限制中发现的*_MAX宏。h INT_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

#9


1  

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;
}

#1


27  

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

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

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

#2


26  

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

有一个文件叫做极限。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

#3


9  

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)必然是最大的价值可能的类型。

#4


6  

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:

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

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.

分别为变量赋值SIZE_MAX、UINT_MAX和ULONG_MAX。一般来说,应该包括限制。使用适当的宏,但是很高兴知道上面的规则。同样,SIZE_MAX不在C89中,所以size_t SIZE_MAX = -1;将在C89和C99工作。

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

注意,溢出行为只对无符号整型进行保证。

#5


4  

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

由于这个问题有一个c++标记,我建议使用numeric_limit:

#include <limits>

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

#6


4  

Another portable way to get maximum value of unsigned integer - is to set all bits to one:

另一种获取无符号整数最大值的可移植方法是将所有位设置为1:

unsigned int uMax = ~0;

#7


2  

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.

通常这是在1.0/0.0处完成的,但是您可能会收到一个编译警告。我不知道在C89中还有其他可移植的方法,但是C99在math.h中有宏FP_INFINITE。

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

编辑:很明显Sam并不想要无穷大,而是要整数限制,可以在limit中找到。h就像其他人说的那样。

#8


1  

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.

我通常使用在限制中发现的*_MAX宏。h INT_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

#9


1  

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;
}