Where are MIN and MAX defined in C, if at all?
What is the best way to implement these, as generically and type safely as possible? (Compiler extensions/builtins for mainstream compilers preferred.)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
They aren’t.
As functions. I wouldn’t use macros like
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standardfmaxorfmin, or fix the macro using GCC’s typeof (you get typesafety bonus too) in a GCC statement expression:Everyone says “oh I know about double evaluation, it’s no problem” and a few months down the road, you’ll be debugging the silliest problems for hours on end.
Note the use of
__typeof__instead oftypeof: