Any C library which defines a macro named max
in its standard headers is broken beyond imagination. Fortunately, an easy workaround if you need to support such platforms is to #undef max
(and any other problematic macros it defines) after including the system headers and before any of your own headers/code.
Note that everyone else is saying to wrap your definition in #ifndef max ... #endif
. This is not a good idea. Defining max
in a system header is an indication that the implementor was incompetent, and it's possible that certain versions of the environment have incorrect macros (for example, ones which do not properly protect arguments with parentheses, but I've even seen a max
macro that was incorrectly performing min
instead of max
at least once in my life!). Just use #undef
and be safe.
As for why it's so broken for stdlib.h
to define max
, the C standard is very specific about what names are reserved for the application and what names are reserved for standard functions and/or internal use by the implementation. There are very good reasons for this. Defining macro names in system headers that could clash with variable/function names used in the application program is dangerous. In the best case it leads to compile-time errors with an obvious cause, but in other cases it can cause very strange behavior that's hard to debug. In any case it makes it very difficult to write portable code because you never know what names will already be taken by the library.