MIN and MAX in C

后端 未结 14 1546
攒了一身酷
攒了一身酷 2020-11-22 13:32

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? (Compil

14条回答
  •  余生分开走
    2020-11-22 14:22

    There's a std::min and std::max in C++, but AFAIK, there's no equivalent in the C standard library. You can define them yourself with macros like

    #define MAX(x, y) (((x) > (y)) ? (x) : (y))
    #define MIN(x, y) (((x) < (y)) ? (x) : (y))
    

    But this causes problems if you write something like MAX(++a, ++b).

提交回复
热议问题