With a maximum number of two numbers in a macro implementation requirements

With a maximum number of two numbers in a macro implementation requirements

In the interview or written examination, often encounter "with a macro seek to achieve the maximum number of two numbers in the" subject, under normal circumstances, we see this problem, I feel very easy to implement, what is difficult to do, it is readily One:

#define MAX(x, y) \
        ((x) > (y) ? (x) : (y))

Note: enclose the entire macro definition at any time, it is a good habit in parentheses.
If you can write on top of this macro, you will be able to cross examination of solving the problem, and then think for oneself is a handy thing to write, it may be wrong. Because of the wording of the above macro definition, although it can get a score, but the interviewer or written by marking it seems, you are better than this, you just vast sea of ordinary one. So for this track bland topic, how to give the examiner a shines, suddenly the impression that the use of the following are several ways you can try to achieve next.

On top of that macro definition, under normal circumstances, it is to meet the demand, but for some cases parameters have side effects, it is easy to unexpected results emerged. such as:

int a = 1;
int b = 10;
int max = MAX(a++, b++);
// 宏定义展开:
((a++) > (b++) ? (a++) : (b++));

The above example, the result will be based on the difference compiler produce some unexpected results, these will not be application developers want, they can ponder the next ...

In order to prevent the side effects of the presence of two macro definition parameters, the parameters can be passed to a macro definition, before the comparison, one back-up, to compare the parameter with the backup, it can not be wrong, and achieved, the parameters side effects is calculated only once, not affect the result of comparison, the following implementation MAX_2:

#define MAX_2(x, y) ({\
        int _x = (x); \
        int _y = (y); \
        _x > _y ? _x : _y; \
})

However, it was soon found that more than MAX_2 macro definition, is only used when comparing two int type parameters, the actual situation may contrast unsigned char, or other type, then the macro definition can not be expected to achieve good results .
So, to continue to improve, the type of parameters to be passed to the comparison in the form of a macro definition of parameters, such as the following MAX_3:

#define MAX_3(type, x, y) ({\
        type _x = (x); \
        type _y = (y); \
        _x > _y ? _x : _y; \
})

Thus, the macro parameters define parameters of two types to be compared, passed as arguments to macro definitions, macro definitions, the parameter type, parameter types that will be used in the method of passing macro definitions are as follows:

unsigned char c = 'A';
unsigned char d = 'B'

MAX_3(unsigned char, c, d);

MAX_3 macro definition, seeking to achieve a good maximum of two parameters for different types of functions, but the first not too happy, because MAX_3 still exist some shortcomings, such as, for some carelessness, resulting in two parameters passed, inconsistency exists and a case where the parameter type, as follows:

int a = 100;
unsigned char c = 'H';

MAX_3(unsigned char, a, c);

Above, may be just wrong hands, but this accident did exist, but MAX_3 macro definition also does normal execution, but the results may not achieve the intended person, and in the code is difficult to be checked out, we should have spend a lot of time to check the Bug, and finally found a small symbol error, so helpless, say no more ...
the above, there is also a method, such as the following MAX_4 macro definitions:

#define MAX_4(x, y) ({\
        typeof(x) _x = (x); \
        typeof(y) _y = (y); \
        (void)(&_x == &_y); \
        _x > _y ? _x : _y; \
})

MAX_4 macro definition, a keyword is acquired by typeof type parameter, and save a copy of the parameter, the parameter to prevent side effects comparison results, and then by (void) (& _ x == & _y); to compare two parameter types, if is not the same type, it will be reported at compile time warning, causing developers to pay attention to, eliminate hidden dangers in advance.

In summary, if the candidate can quickly write implementation MAX_4 macros defined in the written test, I believe absolutely give the examiners themselves, even amazing results.
If the above are four ways to achieve the effect you want, then I have no idea, because MAX_4 macro definition can be said to be within the scope of my knowledge, the safest achieve "maximum macro definition in the sum of two numbers "way to go. Always welcome friends to achieve a better way to learn.

Guess you like

Origin www.cnblogs.com/microxiami/p/11073981.html