# include <stdio.h>
# define scanf "%s Hello World"
int main (void)
{
printf(scanf, scanf);
getchar();
return 0;
}
In the above code snippet, before the code is executed, the macro expansion phase takes place. In this, each occurrence of 'scanf' is replaced by "%s Hello World".
Hence printf(scanf,scanf) will become
printf(“%s Hello World” , “%s Hello World”).
The final output will be :
%s Hello World Hello World
Now, no problems were encountered in this program because we did not use the scanf function here.
However, if we use the scanf function for some purpose
# include <stdio.h>
# define scanf "%s Hello World"
int main(void)
{
int x;
printf(scanf, scanf);
scanf("%d",&x);
getchar();
return 0;
}
We encounter an error :
main.c: In function ‘main’:
main.c:2:17: error: called object is not a function or function pointer
# define scanf "%s Hello World"
^
main.c:7:4: note: in expansion of macro ‘scanf’
scanf("%d",&x);
^~~~~
Why does then C allow us to name macros after library functions like Scanf in the first place ?