Why is the 'max' macro defined like this in C?

后端 未结 4 531
故里飘歌
故里飘歌 2021-01-05 02:17
 #define max(a,b) \\
   ({ typeof (a) _a = (a); \\
       typeof (b) _b = (b); \\
     _a > _b ? _a : _b; })

Why not simply (a>b ? a :

4条回答
  •  醉酒成梦
    2021-01-05 02:58

    If the macro is called with expression, this might lead to unexpected behaviour. Assume this:

    int c = max(i++, j++);

    In this case, the max is increase twice with the simpler version.

提交回复
热议问题