Home > Blockchain >  Difference between INT_MAX and __INT_MAX__ in C
Difference between INT_MAX and __INT_MAX__ in C

Time:12-17

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?

CodePudding user response:

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. It is simply an implementation detail of the compiler that you are using. Other compilers will probably not define this macro, and will implement INT_MAX in some other way.

CodePudding user response:

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

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related