Can a function name and function macro be the same?

Can a function name and function macro be the same?
Wouldn't that cause any problems?

+3
source share
3 answers

They may be the same. Depending on how you use the name, it is either replaced by the preprocessor or not. for instance

//silly but just for demonstration.
int addfive(int n)
{
    return n + 5;
}
#define addfive(n) ((n) + 5)

int main(void)
{
    int a;
    a = addfive(2); //macro
    a = (addfive)(2); //function
}

for ex. MS says that: http://msdn.microsoft.com/en-us/library/aa272055(v=vs.60).aspx

+8
source

http://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros

Here you can see that calling a function from which a macro with the same name exists calls a macro :) For gcc at least!

, . .

+1

I will explain through cases:
If you first declared a function, then the function, for example, the second macro, the macro will execute the function. that is, it will always be called instead of a function.

//Function
double squar(double x)
{
    return x*x;
}

//Macro
#define squar(x) (x*x)

On the other hand, if you declare a macro first and then a function, an exception occurs, you cannot build

//Macro
#define squar(x) (x*x)

//Function
double squar(double x)
{
    return x*x;
}

In the end, in the first case, you still call the function, as @Hayri Uğur Koltuk said here in his answer(squar)(5)

+1
source

All Articles