3 ms·
"min" is a pathological case for a macro because it isn't what macros were meant for. Tell that to Kernighan and Ritchie. The second macro in K&R (I googled a
by Someone 12y ago
"min" is a pathological case for a macro because it isn't what macros were meant for.
Tell that to Kernighan and Ritchie. The second macro in K&R (I googled a PDF of the second edition) is
#define max(A, B) ((A) > (B) ? (A) : (B))
Yes, that's lowercase 'max', and they even mention "This macro will work for any data type; there is no need for different kinds of max for different data types, as there would be with functions"
Macros certainly were intended for this, as it was the only way to guarantee that the compiler inlined code (probably the only way it ever inlined function calls)
And for the curious: the first macro they give is
#define forever for(;;)
That certainly is what macros were meant for :-), and doesn't even save on typing.
But yes, in modern C, nobody would use a macro for this.
- hornetblack 12y agoThe issue is for some programmers propensity to use ++ and -- whenever they can. So you end up with max(a++, b--) Which expands to ((a++)>(b--)?(a++):(b--)) creating unexpected results. The solution is, don't use return values of `++` and `--` in calls, unless you know it's a function, and always will be. Upper-case macros may help, but then you need to make sure all macros are upper case.