473,324 Members | 2,002 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,324 software developers and data experts.

want to know hoe the following works .

#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

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

the output of this is
12
f(1,2)
on executing the code .Can someone pleease tell me how this works ??
Thanking you,
ranjan.

Nov 15 '05 #1
4 1356
Hi Ranjan,

If an occurrence of a parameter in the replacement token is preceded by
#, quotes (") will be placed around the corresponding parameter.

h(f(1,2)) ===> first f(1,2) will be expanded as 12 ===> h(12) ===>
g(12) ===> "12"
g(f(1,2)) ===> "f(1,2)"

Regards,
Raju

maadhuu wrote:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

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

the output of this is
12
f(1,2)
on executing the code .Can someone pleease tell me how this works ??
Thanking you,
ranjan.


Nov 15 '05 #2
maadhuu wrote:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)

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

the output of this is
12
f(1,2)
on executing the code .Can someone pleease tell me how this works ??
Thanking you,
ranjan.


Read a C book on the preprocessor operators # (stringize) and
## (concat) for details; the former creates a string literal
from the operand, the latter concatenates its operands.
The # operator needs to be wrapped once (here by h) in order
to "evaluate" the operand before stringizing it.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #3
hi, i still have a doubt . i know that "#" is the stringinizing operator
and "##" is the concatenation operator........now, if the inner operation
is performed first as in, in the 1st case f(1,2) and in the 2nd case also,
f(1,2) and then the outer operation, don't you think that the output should
be the same .........i want to know how does one extra layer in case of
h(f(1,2)) make the difference ........thanking you, coz' it seemed to me
that both h(f(1,2)) and g(f(1,2)) should give the same output.
thanking you, once again.
ranjan.

Nov 15 '05 #4
maadhuu wrote:
hi, i still have a doubt . i know that "#" is the stringinizing operator
and "##" is the concatenation operator........now, if the inner operation
is performed first as in, in the 1st case f(1,2) and in the 2nd case also,
f(1,2) and then the outer operation, don't you think that the output should
be the same .........i want to know how does one extra layer in case of
h(f(1,2)) make the difference ........thanking you, coz' it seemed to me
that both h(f(1,2)) and g(f(1,2)) should give the same output.


Please provide _context_!
Without it, you make it unnecessarily hard for other people to help you.

Your original message contained
,----
|#include <stdio.h>
| #define f(a,b) a##b
| #define g(a) #a
| #define h(a) g(a)
|
| int main()
| {
| printf("%s\n",h(f(1,2)));
| printf("%s\n",g(f(1,2)));
| return 0;
| }
`----

The "extra layer" makes all the difference because preprocessor
operation is essentially text replacement:
g(f(1,2)) becomes #f(1,2) because this is how the # operator works.
h(f(1,2)) becomes g("Expand(f(1,2))") i.e. g(12) because this is
how macro treatment in the absence of # works.
So, the additional "layer" leads to actual expansion of f(1,2).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Atul | last post by:
Hi, I just stepped into C#. I am facing one problem. Here is the scenario. I am posting some data to one website (https site). It returns me response HTML. I want to parse this HTML and want to...
182
by: Jim Hubbard | last post by:
http://www.eweek.com/article2/0,1759,1774642,00.asp
14
by: Professor Yonce | last post by:
I have made form for E-Mail. I have entered code but the Import system does not work. It has squiggly line underneath it showing it is not communicating. It Will not build. Public Class...
21
by: Lee.kain | last post by:
I want to learn C++! does anyone have any advice? Lee
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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...
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)...
1
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.