Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro function call from within printf statement in C

I am facing a problem with understanding the use of macro function calls from within a printf() statement.

I have the code below :

#include<stdio.h>
#define f(g,h) g##h
main()
{
    printf("%d",f(100,10));
}

This code outputs "10010" as the answer.

I have learned that macro function call simply copy pastes the macro function code in place of the call with the arguments replaced.

So the code should be like :

#include<stdio.h>
#define f(g,h) g##h
main()
{
    printf("%d",100##10);
}

But when i executed the above code separately with substituted macro,i get a compilation error.

So how does the first code gives 10010 as the answer while the second code gives a compilation error?

like image 643
KP_K Avatar asked Nov 26 '12 09:11

KP_K


1 Answers

The preprocessor concatenation operator ## is done before the macro is replaced. It can only be used in macro bodies.

like image 58
Some programmer dude Avatar answered Nov 15 '22 09:11

Some programmer dude