Documente Academic
Documente Profesional
Documente Cultură
WhatisthetokenpastingoperatorandstringizingoperatorinC?|TheWarrior
TheWarrior
ThebaleeldforwarriorinI.Tworld
Whatisthetokenpastingoperatorandstringizingoperator
inC?
Tokenpastingoperator
ANSIhasintroducedawelldenedtokenpastingoperator,##,whichcanbeusedlikethis:
#definef(g,g2)g##g2
main()
{
intvar12=100;
printf("%d",f(var,12));
}
O/P
100
Stringizingoperator
#definesum(xy)printf(#xy"=%f\n",xy);
main()
{
sum(a+b);//Asgoodasprintf("a+b=%f\n",a+b);
}
Sowhatdoesthemessagewarning:macroreplacementwithinastringliteralmean?
https://kunaliiita.wordpress.com/2007/09/01/whatisthetokenpastingoperatorandstringizingoperatorinc/
1/4
12/3/2016
WhatisthetokenpastingoperatorandstringizingoperatorinC?|TheWarrior
#defineTRACE(var,fmt)printf("TRACE:var=fmt\n",var)
TRACE(i,%d);
getsexpandedas
printf("TRACE:i=%d\n",i);
In other words, macro parameters were expanded even inside string literals and character constants.
Macro expansion is *not* dened in this way by K&R or by Standard C. When you do want to turn
macroargumentsinto
strings,youcanusethenew#preprocessingoperator,alongwithstringliteralconcatenation:
#defineTRACE(var,fmt)printf("TRACE:"#var"="#fmt"\n",var)
Seeandtrytounderstandthisspecialapplicationofthestrigizingoperator!
#deneStr(x)#x
#deneXstr(x)Str(x)
#deneOPplus
char*opname=Xstr(OP);//ThiscodesetsopnametoplusratherthanOP.
Herearesomemoreexamples
Example1
DefineamacroDEBUGsuchthatthefollowingprogram
intmain()
{
intx=4;
floata=3.14;
charch='A';
DEBUG(x,%d);
DEBUG(a,%f);
DEBUG(ch,%c);
}
outputs
DEBUG:x=4
DEBUG:y=3.140000
DEBUG:ch=A
Themacrowouldbe
#defineDEBUG(var,fmt)printf("DEBUG:"#var"="#fmt"\n",var);
https://kunaliiita.wordpress.com/2007/09/01/whatisthetokenpastingoperatorandstringizingoperatorinc/
Example2
2/4
12/3/2016
WhatisthetokenpastingoperatorandstringizingoperatorinC?|TheWarrior
Example2
WriteamacroPRINTforthefollowingprogram
intmain()
{
intx=4,y=4,z=5;
inta=1,b=2,c=3;
PRINT(x,y,z);
PRINT(a,b,c);
}
suchthatitoutputs
x=4y=4z=5
a=1b=2c=3
Hereisamacrothatwilldothis
#definePRINT(v1,v2,v3)printf("\n"#v1"=%d"#v2"=%d"#v3"=%d",v1,v2,v3)
Abouttheseads(https://wordpress.com/abouttheseads/)
92%
RoundDialMulticolorLeather
StrapWomensQuartzWatc
Rs.99
14%
ORDER
LancerMensBlueandNeon
GreenLaceUpSportsShoes
Rs.425
ORDER
This entry was posted on September 1, 2007 at 7:41 pm and is led under C brainstorming. You can
followanyresponsestothisentrythroughtheRSS2.0feed.Youcanleavearesponse,ortrackbackfrom
yourownsite.
5ResponsestoWhatisthetokenpastingoperatorandstringizing
operatorinC?
ShashankSays:
December22,2010at12:44pm|Reply
wellexplained
https://kunaliiita.wordpress.com/2007/09/01/whatisthetokenpastingoperatorandstringizingoperatorinc/
kbcbccSays:
3/4
12/3/2016
WhatisthetokenpastingoperatorandstringizingoperatorinC?|TheWarrior
kbcbccSays:
July31,2011at12:47pm|Reply
tellme,watisuseofmacropasting??
EnesSays:
November19,2011at12:36pm|Reply
thanksforwriting
holidaysSays:
February4,2012at9:35pm|Reply
tickets
[]WhatisthetokenpastingoperatorandstringizingoperatorinC?TheWarrior[]
arunkumarRSays:
February13,2012at6:24am|Reply
goodexample
CreateafreewebsiteorblogatWordPress.com.
Entries(RSS)andComments(RSS).
https://kunaliiita.wordpress.com/2007/09/01/whatisthetokenpastingoperatorandstringizingoperatorinc/
4/4