473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Question on macro substitution

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
2 3064

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Raghuveer Pallikonda | last post by:
Hi, I am trying to stub out debug log messages in my application, if the logging subsystem is not enabled.. For e.g a invocation #define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg so...
8
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
1
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
5
by: vineoff | last post by:
How can macro take a number in this way (or can it) : #define MYMACRO(x) (x) int val = 5; MYMACRO(val); seems it produces val.
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.