49

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?

3
  • 1
    There is some good information about leading underscores in this SO question.
    – AdrianHHH
    Dec 17, 2022 at 12:16
  • Please apply specific tags (eg what language) to your question.
    – MoMo
    Dec 22, 2022 at 15:40
  • 1
    @MoMo I have done that since I posted it wdym? Dec 26, 2022 at 11:22

8 Answers 8

57

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.

0
43

__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.

5
  • 3
    Clang also defines INT_MAX, as part of its "Works a lot like GCC" functionality, Dec 17, 2022 at 18:01
  • 4
    I think that’s more “works like a c compiler” -)
    – gnasher729
    Dec 18, 2022 at 1:16
  • 7
    @gnasher729: I think John Dallman wrote INT_MAX (with leading and trailing underscores) but he meant __INT_MAX__ (with enclosing back-ticks) instead. Dec 18, 2022 at 13:12
  • 4
    test_with_leading_and_trailing_underscores
    – Oskar Skog
    Dec 18, 2022 at 16:32
  • 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. Dec 18, 2022 at 20:14
15

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.

11

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.

0
1

__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.

5
  • 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. Dec 18, 2022 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.) Dec 19, 2022 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, 2022 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… Dec 20, 2022 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.” Dec 20, 2022 at 19:14
1

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.

1
  • 2
    Okay, but you need to explain how INT_MAX is different from __INT_MAX__.
    – Null
    Dec 22, 2022 at 20:52
0

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.

-1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.