与宏同名的函数

function with the same name as a macro

本文关键字:函数      更新时间:2023-10-16
#include<stdio.h>
void f(int a)
{
printf("%d", a);
}
#define f(a) {}
int main()
{
 /* call f : function */
}

如何调用f(函数(?写入f(3)不起作用,因为它被{} 取代

(f)(3);工作吗?

C预处理器不在( )中展开宏f


int main()
{
#undef f  // clear f!
 f(3);
}

使用函数指针来实现这一点:

int main() {
    void (*p)(int a);
    p = f;
    p(3); //--> will call f(3)
    return 0;
}

一个解决方案由@Prasoon发布,另一个可能只是为函数引入另一个名称,如果你不能更改函数的名称,也不能更改宏的名称:

#include<stdio.h>
void f(int a)
{
   printf("%d", a);
}

#define fun (f) //braces is necessary 
#define f(a) {}
int main()
{
     fun(100);
}

在线演示:http://www.ideone.com/fbTcE