By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,959 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,959 IT Pros & Developers. It's quick & easy.

Question on macro substitution

P: n/a
Hi all,
I have the following code ,

#include <stdio.h>

#define f(a,b) a##b
#define g(a) #a

int main(void)
{
printf("%s\n",g(f(10,20)));
return 0;
}

Output that i got is
f(10,20)
But i expected
1020

My assumption was
1) first f(10,20) is replaced by 1020 , because of macro f(a,b)
concantenates its arguments
2) then g(a) macro replaces 1020 by a string literal "1020"
3) the output should be 1020

But why is f(10,20) not getting substituted.
I'm sure i'm missing something here.
Can someone explain me this program ?
Thanks for your time,
Yugi.

Sep 19 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

main() wrote:
Hi all,
I have the following code ,

#include <stdio.h>

#define f(a,b) a##b
#define g(a) #a

int main(void)
{
printf("%s\n",g(f(10,20)));
return 0;
}

Output that i got is
f(10,20)
But i expected
1020

My assumption was
1) first f(10,20) is replaced by 1020 , because of macro f(a,b)
concantenates its arguments
2) then g(a) macro replaces 1020 by a string literal "1020"
3) the output should be 1020

But why is f(10,20) not getting substituted.
I'm sure i'm missing something here.
Can someone explain me this program ?
Thanks for your time,
Yugi.
I came across one more piece of code that is even more confusing..

#include <stdio.h>

#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

int main(void)
{
printf("%s\n",h(f(1,2)));
printf("%s\n",g(f(1,2)));
return 0;
}

output is
12
f(1,2)

I'm totally lost.
I searched in this newsgroup. It has been answered before. But i
couldn't understand it.
Please can someone explain what exactly is happening here ?

Sep 19 '06 #2

P: n/a
main() wrote:
Hi all,
I have the following code ,

#include <stdio.h>

#define f(a,b) a##b
#define g(a) #a

int main(void)
{
printf("%s\n",g(f(10,20)));
return 0;
}

Output that i got is
f(10,20)
But i expected
1020

My assumption was
1) first f(10,20) is replaced by 1020 , because of macro f(a,b)
concantenates its arguments
2) then g(a) macro replaces 1020 by a string literal "1020"
3) the output should be 1020

But why is f(10,20) not getting substituted.
I'm sure i'm missing something here.
Can someone explain me this program ?
Thanks for your time,
Yugi.
This is a FAQ. Read Question 11.17 from http://c-faq.com/index.html

Krishanu
Sep 19 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.