Wednesday, April 27, 2005

Function and Macro with the same name

Although the situation where you have a function and a macro with the same name is not common in programming, the possibility cannot be ruled out. The compiler will not complain if the macro is defined after the function.

Can one resolve the call to ensure that the function gets called?

Here is an example of how to do so

#include < stdlib.h >

max(int a, int b)
printf("calling function max\n");
return ((a > b) ? a : b);

#define max(a, b) ((a > b) ? (a) : (b))

int i = 10, j = 5;
printf("1) %d\n", max(i, j));
i = 5;
j = 10;
printf("2) %d\n", (*max)(i, j));

return 0;

The good thing about C programming is that the function name also acts a function pointer. In addition, a function can be called by its name or called by dereferencing the function pointer.
Post a Comment