How would you set a variable to equal infinity (or any guaranteed largest number value) in C?
-
4What kind of variable? int, float etc? – Andrew Feb 16 '10 at 15:25
-
3I have removed the C++ tag, the question is specifically asking for C, and in this particular case the idiomatic way of doing it in C++ differs from the C approach (There is one answer that deals with C++ here: http://stackoverflow.com/questions/2273913/how-would-you-set-a-variable-to-the-largest-number-possible-in-c/2274041#2274041). – David Rodríguez - dribeas Feb 16 '10 at 15:44
10 Answers
#include <limits.h>
int x = INT_MAX;
EDIT: answered before the questioner clarified, I was just guessing what type they wanted.

- 4,722
- 1
- 36
- 34
-
1thanks for the accept, but I think amo-ej1's answer is more complete. – dubiousjim Feb 16 '10 at 15:41
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

- 3,279
- 26
- 35
-
8Be sure not to copy&paste the values from here, but instead #include
. The header is part of C89 and thus it is fully portable. – Tronic Feb 16 '10 at 15:33 -
The same as online reference: http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html#limits.h – h0b0 Feb 16 '10 at 16:42
-
1Also interesting to note is how `INT_MIN` is defined on this particular system. They can't define it as `-2147483648`, because that would mean "negative 2147483648", and 2147483648 overflows an `int`! – Alok Singhal Feb 16 '10 at 19:09
-
1Also interesting to note is how INT_MIN gets derived from INT_MAX before this is defined. Must be a copy-paste error ... – Jongware Dec 10 '13 at 21:41
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

- 10,935
- 5
- 50
- 70
-
2Your comment is interesting, can you please explain how you came to this solution? – highlander141 Apr 24 '16 at 10:06
-
I think this should definitely be the top answer, i've tested it with uchar, ushort, uint, ulong, and even variable-bit length variables and they all fill with 1s. – Mark Walsh Aug 09 '18 at 17:34
-
Since ints are signed by default, could you do `int iMax = (unsigned int)~0 >> 1;` instead of `signed int iMax = (unsigned int)~0 >> 1;`? – Mike Smith Jun 03 '20 at 22:52
-
@BobRobert Probably it should work, just some examples was for unsigned integers, so I wanted my intentions to be more clear. Btw, in C89 standard there _may be_ some subtle differences in [bit-field declarations](https://stackoverflow.com/questions/44624857/is-int-always-signed). – Agnius Vasiliauskas Jun 04 '20 at 05:46
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.

- 476,176
- 80
- 629
- 1,111
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.

- 93,253
- 21
- 125
- 158
-
1Yes, this is good to know for cases where you don't want to rely on the macro being defined. But is it best coding practice? Someone else might look at the code and be unsure whether there was a mistake (perhaps coming from careless revisions). Using the macros helps document your intent. – dubiousjim Feb 16 '10 at 16:35
-
1@profjim: as I said, using macros is better, but in case you can't (C89 size_t for example), you should use `-1`. – Alok Singhal Feb 16 '10 at 19:07
Since there's a C++ tag on this question, I'll suggest numeric_limits:
#include <limits>
unsigned x = std::numeric_limits<unsigned>::max();

- 60,987
- 18
- 112
- 174
-
2Before anybody down votes me, there WAS a C++ tag until it was removed by David Rodríguez. Looking at the edit history, I see it was added by someone other than the OP. – Fred Larson Feb 16 '10 at 15:54
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.

- 10,250
- 2
- 41
- 53
-
Hmm didn't think about checking math.h for something like that. This will definitely work. Thanks! EDIT: limits.h works better in my case, sorry for the confusion lol – Sam Feb 16 '10 at 15:30
-
1`1.0/0.0` is not portable, and in fact constructing infinity bitwise using a union with `float` and `uint32_t` is probably more portable than division by zero. – R.. GitHub STOP HELPING ICE Sep 21 '10 at 02:41
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

- 570
- 4
- 11
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;
}

- 2,783
- 5
- 18
- 31

- 658
- 1
- 8
- 28
- 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//

- 77
- 5