Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   want to know hoe the following works . (http://www.velocityreviews.com/forums/t439065-want-to-know-hoe-the-following-works.html)

maadhuu 08-13-2005 05:34 AM

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.


g.kanaka.raju@gmail.com 08-13-2005 07:50 AM

Re: want to know hoe the following works .
 
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.



Michael Mair 08-13-2005 07:51 AM

Re: want to know hoe the following works .
 
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.

maadhuu 08-13-2005 11:58 AM

Re: want to know hoe the following works .
 
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.


Michael Mair 08-13-2005 12:24 PM

Re: want to know hoe the following works .
 
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.


All times are GMT. The time now is 01:40 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.