First, you should have a very good reason to choose a macro like function over an actual function. If you had used a function, you would be able to specify that the function parameters should be double
more naturally.
void logic (double x, double y) {
if (x > y) {
printf("%lf\n", x);
} else {
printf("%lf\n", y);
}
}
Since you are using a macro, there is no type specified for the arguments. So, if you pass in an int
argument to the macro, that is how it is treated in the expansion. Undefined behavior results when printf
is told to expect a double
parameter when an int
was passed in instead.
One way to fix the macro is to assign the parameters to local variables with the appropriate type, which better emulates what an actual function would do.
#define LOGIC(X,Y) do {\
double XX = (X); \
double YY = (Y); \
if(XX>YY)\
printf("%lf",XX);\
else\
printf("%lf",YY);\
} while (0)