# include <stdio.h>
# define scanf "%s Geeks For Geeks "
main()
{
printf(scanf, scanf);
getchar();
return 0;
}
在上面的代码片段中,在执行代码之前,宏扩展阶段已开始。在这种情况下,每次出现的“ scanf”都将替换为“%s Geeks For Geeks”
因此,printf(scanf,scanf)将变为 printf(“%s Geeks For Geeks”,“%s Geeks For Geeks”)
最终输出将是: %s极客,极客极客,极客
现在,在此程序中没有遇到任何问题,因为我们在这里没有使用scanf函数。
但是,如果我们出于某些目的使用scanf函数
# include <stdio.h>
# define scanf "%s Geeks For Geeks "
int main()
{
int x;
printf(scanf, scanf);
scanf("%d",&x);
getchar();
return 0;
}
我们遇到一个错误:
main.c: In function ‘main’:
main.c:2:17: error: called object is not a function or function pointer
# define scanf "%s Geeks For Geeks "
^
main.c:7:4: note: in expansion of macro ‘scanf’
scanf("%d",&x);
^~~~~
那么,为什么C首先允许我们使用诸如Scanf之类的库函数来命名宏?