What is the difference between the 2? __INT_MAX__
is defined without adding a library as far as I know and INT_MAX
is defined in limits.h
but when I include the library INT_MAX
gets expanded to __INT_MAX__
either way (or so does VSCode say). Why would I ever use the limits.h
one when it gets expanded to the other one?

- 328,167
- 45
- 605
- 847

- 1,628
- 2
- 7
- 13
-
1There is some good information about leading underscores in [this SO question](https://stackoverflow.com/questions/25090635/use-and-in-c-programs). – AdrianHHH Dec 17 '22 at 12:16
-
Please apply specific tags (eg what language) to your question. – MoMo Dec 22 '22 at 15:40
-
1@MoMo I have done that since I posted it wdym? – Thanos Kyprianos Dec 26 '22 at 11:22
8 Answers
You should always use INT_MAX
, as that is the macro constant that is defined by the ISO C standard.
The macro constant __INT_MAX__
is not specified by ISO C, so it should not be used, if you want your code to be portable. That macro is simply an implementation detail of the compiler that you are using. Other compilers will probably not define that macro, and will implement INT_MAX
in some other way.

- 22,760
- 4
- 24
- 39
__INT_MAX__
is an implementation defined macro, which means not all systems may have it. In particular, GCC defines this macro but MSVC does not.
On the other hand, INT_MAX
is defined by the C standard and is guaranteed to be present in limits.h
for any conforming compiler.
So for portability, use INT_MAX
.

- 205,898
- 23
- 218
- 273
-
3Clang also defines __INT_MAX__, as part of its "Works a lot like GCC" functionality, – John Dallman Dec 17 '22 at 18:01
-
4
-
7@gnasher729: I think John Dallman wrote __INT_MAX__ (with leading and trailing underscores) but he meant `__INT_MAX__` (with enclosing back-ticks) instead. – Edgar Bonet Dec 18 '22 at 13:12
-
4
-
One *could* also use backslashes: `\_\_INT_MAX\_\_` ⇒ \_\_INT_MAX\_\_, or even `_\_INT_MAX_\_` ⇒ _\_INT_MAX_\_. But, yeah, since we’re talking about code, we should use code formatting. – Scott - Слава Україні Dec 18 '22 at 20:14
Why would I ever use the limits.h one when it gets expanded to the other one?
limits.h
is standard and portable.
Every implementation of the C language is free to create the value of macros such as INT_MAX
as it sees fit. The __INT_MAX__
value you are seeing is an artifact of your particular compiler, and maybe even the particular version of the compiler you're using.

- 32,625
- 3
- 24
- 56
To add to the other answers, when you're writing code that will be run on several platforms, it really pays to stick to the standards. If you don't, when a new platform comes along, you have a lot of work to do adapting it, and the best way to do that is usually to change it conform to the standard. This work is very dull and uninteresting, and well worth avoiding by doing things right to start with.
I work on a mathematical modeller that was originally written in the 1980s on VAX/VMS, and in its early days supported several 68000 platforms, including Apollo/Domain. Nowadays, it runs on 64-bit Windows, Linux, macOS, Android and iOS, none of which existed when it was created.

- 590
- 4
- 18
__INT_MAX__
is a predefined macro in the C preprocessor that specifies the maximum value of an int type on a particular platform. This value is implementation-defined and may vary across different platforms.
INT_MAX
is a constant defined in the limits.h header file that specifies the maximum value of an int type. It is defined as follows:
define INT_MAX __INT_MAX__
The limits.h header file is part of the C standard library and provides various constants that specify the limits of various types, such as the minimum and maximum values of the int, long, and long long types.
The reason why INT_MAX
is defined as __INT_MAX__
is because __INT_MAX__
is a predefined macro that specifies the maximum value of an int type on a particular platform, and INT_MAX is simply an alias for this value.
You can use either __INT_MAX__
or INT_MAX
to get the maximum value of an int type, but it is generally recommended to use INT_MAX
since it is defined in a standard library header file and is therefore more portable.
-
7`__INT_MAX__` is not implementation-defined. That term, in the context of C, means the C standard **requires** an implementation to specify what the definition is. The C standard makes no such requirement for `__INT_MAX__`. An implementation **may** define `__INT_MAX__`, but the term “implementation-defined” should not be used to describe that. – Eric Postpischil Dec 18 '22 at 16:20
-
1`__INT_MAX__` is a reserved name for the implementation's internal use; note the two leading underscores followed by a capital letter. What you're describing is only GCC / glibc's implementation choice. (Where they put the actual numeric constant in the compiler, instead of needing different versions of `limits.h` for targets with 16-bit `int`, or 32 vs. 64-bit `long`.) – Peter Cordes Dec 19 '22 at 19:48
-
@EricPostpischil what's the correct term for this concept? I would have used "implementation defined" to mean any feature a particular implementation supports and documents, not necessarily something required by the standard. – PC Luddite Dec 20 '22 at 19:05
-
@PCLuddite: Mostly I would just avoid the specific phrasing “implementation-defined,” since the C standard uses it to mean things an implementation is required to define. I might say “GCC defines `__INT_MAX__` to be the maximum value of an `int`” or “Some C implementations define `__INT_MAX__`,” and so on. I am not sure GCC actually documents `__INT_MAX__` for ordinary programmer use. It may be something it defines for C standard library implementors to use in the headers, so they can write `#define INT_MAX __INT_MAX__` to make a header that adapts to different targets… – Eric Postpischil Dec 20 '22 at 19:13
-
… In general, I might say an implementation defines something as an extension, or that is specifies a certain things, or other phrasing suitable to the particular issue. Just avoid “implementation-defined.” – Eric Postpischil Dec 20 '22 at 19:14
INT_MAX is a macro that specifies that an integer variable cannot store any value beyond this limit.
INT_MIN specifies that an integer variable cannot store any value below this limit.
Values of INT_MAX and INT_MIN may vary from compiler to compiler. Following are typical values in a compiler where integers are stored using 32 bits.
Value of INT_MAX is +2147483647. Value of INT_MIN is -2147483648.

- 11
- 1
-
2Okay, but you need to explain how `INT_MAX` is different from `__INT_MAX__`. – Null Dec 22 '22 at 20:52
In the C programming language, INT_MAX
is a macro that expands to the maximum value that can be stored in a variable of type int. This value is implementation-defined, meaning that it may vary depending on the specific C implementation being used. On most systems, int is a 32-bit data type and INT_MAX
is defined as 2147483647, which is the maximum value that can be stored in a 32-bit, two's complement integer.
On the other hand, __INT_MAX__
is a predefined macro that represents the maximum value that can be stored in a variable of type int on the system where the C program is being compiled. Like INT_MAX
, the value of __INT_MAX__
is implementation-defined and may vary depending on the specific C implementation being used. However, __INT_MAX__
is set by the compiler during compilation, whereas INT_MAX
is typically defined in a header file (e.g., limits.h) and included in the program at runtime.
In general, it is recommended to use INT_MAX
rather than __INT_MAX__
in C programs, as INT_MAX
is portable and will work on any system, whereas __INT_MAX__
is specific to the system where the program is being compiled.

- 2,970
- 1
- 22
- 22
INT_MAX (macro) : It specifies that any integer variable cannot store values beyond the mentioned limit. whereas INT_MIN specifies that any integer variable cannot store some value that is below the mentioned limit.

- 1
- 4