Can the name of function and function-like macro be same?
Wouldn't this cause any problem?

- 1,051
- 5
- 14
- 26
-
1they could be the same. – Hayri Uğur Koltuk Feb 04 '14 at 14:53
-
3This will cause lots of problems and confusion. Don't do this. – Feb 04 '14 at 14:54
-
1you could use #undef macro-name if you want to make sure the function is always called. – tesseract Feb 04 '14 at 15:01
-
1Read this also [What do the parentheses around a function name mean](http://stackoverflow.com/questions/13600790/what-do-the-parentheses-around-a-function-name-mean) – Grijesh Chauhan Feb 04 '14 at 15:10
3 Answers
They could be the same. Depending on how you use the name, either it gets replaced by preprocessor or not. For example
//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

- 104,019
- 25
- 176
- 264

- 2,970
- 4
- 31
- 60
-
1I simply write this code and it fails to compile: `#include
#define max(a,b) ((a > b) ? a : b) int max(int a, int b) { return ((a > b) ? a : b); } int main(){ int a = 5, b = 3; printf("%d\n", max(a,b)); return 0; } ` – haccks Feb 04 '14 at 14:57 -
2
-
+1 because I learned something new. But please don't ever do this in real code. – Fred Larson Feb 04 '14 at 15:03
-
1@FredLarson, AhmedHandy http://stackoverflow.com/questions/13600790/what-do-the-parentheses-around-a-function-name-mean – Grijesh Chauhan Feb 04 '14 at 15:09
-
@msam it should work regardless of the compiler (given it's c90 compliant) – Hayri Uğur Koltuk Feb 04 '14 at 15:09
-
OK. So the extra paren along with function call (`a = (addfive)(2);`) does the magic here! o.O. +1. Thanks @GrijeshChauhan for the link. – haccks Feb 04 '14 at 15:27
http://gcc.gnu.org/onlinedocs/cpp/Function-like-Macros.html#Function-like-Macros
Here you can see that calling the function, of which a macro with the same name exists, calls the macro instead :) For the gcc at least!
This would cause no problem, but quite some confusions. I wouldn't recommend this.

- 4,977
- 3
- 34
- 53
I will explain via cases:
If you declared the function first then the function like macro second, macro will over take the function. i.e. it will be called always instead of the function.
//Function
double squar(double x)
{
return x*x;
}
//Macro
#define squar(x) (x*x)
On the other hand if you declare the macro first then the function later, an exception will be arise, you wont be able to build
//Macro
#define squar(x) (x*x)
//Function
double squar(double x)
{
return x*x;
}
At the end, in the first case, you still call the function like @Hayri Uğur Koltuk said here in his answer by (squar)(5)

- 1
- 1

- 2,547
- 1
- 17
- 23