want to know hoe the following works .

M

maadhuu

#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.
 
G

g.kanaka.raju

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
 
M

Michael Mair

maadhuu said:
#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
 
M

maadhuu

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.
 
M

Michael Mair

maadhuu said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top