Click here to Skip to main content
Click here to Skip to main content

Macro expansion in VC++

By , 4 Feb 2012
 
Let us assume for some unknown reason, we have to write a macro [yes, yes, I know Macros are evil and can be substituted by in-line and template function friendlies - but just in case] or say if we are trying to understand an existing macro in some legacy code.
 
In these situations, it would be nice if we can see how exactly this macro is expanded by the compiler. This tip suggests a way to do so.
 
Look at this:
 
//C:\Test\main.cpp

#define SQUARE(A) (A * A) 
 
void main(int argc, char* argv[])
{
    int y = 2;
    int s = SQUARE(++y); // Due to this the macro expands in a 
                         // different way and we will get abnormal results
                         // To debug, we have to see how this macro is expanded
                         // by the compiler 
}
 
We all know that the value of s will be 16 (rather than 4). To see how the macro expands,
 
Go to VS command prompt and enter:
 
cl /EP C:\Test\main.cpp > C:\Test\macro.txt
 
This command will expand the macro and dump it in macro.txt. The dump looks like
void main(int argc, char* argv[])
{
    int y = 2;
    int s = (++y * ++y);
 
}
See our macro is expanded.
 
And DO NOT down vote me:
  1. If you do not like macros (I too hate them, but ....)
  2. If this is old trick / re-post (I just want to share)
 
Yes, there could be many alternatives and this is one.
 
Thanks (:-

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Lakamraju Raghuram
Software Developer Triad [ CADCAM-e ]
India India
Member
_____________________________________________________________
 
Did my masters from IIT-M in Advanced Manufacturing Technology and I am working mainly on C++ from 2004 onwards.
 
I like to play with my 16 month old daughter Pranavi [rather she plays with me]. By the way she pronounces C++ as "Chi pee-pee".
 
Hate traveling (but thanks to my wife I traveled to Hyd about 50 times in last 3 years).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 3memberrahman_tanzilur0113 Jan '13 - 11:00 
QuestionVS addinmembersokolov765 Aug '12 - 23:04 
AnswerRe: VS addinmemberLakamraju Raghuram6 Aug '12 - 1:51 
GeneralMy vote of 5memberVolynsky Alex19 May '12 - 1:33 
GeneralRe: This way of analysis is cool .... how many will scroll thoug...memberLakamraju Raghuram31 Jan '12 - 7:03 
GeneralRe: Technically you should wrap the usage of macro arguments wit...memberChuck O'Toole4 Feb '12 - 16:22 
GeneralReason for my vote of 5 Good tipmemberManish K. Agarwal20 Feb '12 - 0:12 
GeneralReason for my vote of 5 Very well explainedmemberJoxemi7 Feb '12 - 4:42 
GeneralReason for my vote of 5 Helped resolve 3rd party library API...membermriddle6 Feb '12 - 13:06 
GeneralIt would add to the article to describe why the result is 16...membernv33 Feb '12 - 2:41 
General(:-memberLakamraju Raghuram31 Jan '12 - 7:02 
GeneralReason for my vote of 5 A nice tip for "What to care of..."memberJose David Pujo31 Jan '12 - 4:59 
GeneralHa! got you buddy.... It is copy-paste problem, I just chec...memberLakamraju Raghuram30 Jan '12 - 8:26 
GeneralRe: +5memberAlbert Holguin30 Jan '12 - 9:33 
GeneralRe: At first glance, I was sure that the result of "s" should be...memberJose David Pujo31 Jan '12 - 4:57 
GeneralI know what I am doing. I intended to show an example where ...memberLakamraju Raghuram29 Jan '12 - 19:07 
GeneralRe: The reason you got a downvote was for not doing this: <code>...memberAlbert Holguin30 Jan '12 - 4:53 
GeneralRe: The reason you got a downvote was for not doing this:...memberPIEBALDconsult27 Jul '12 - 10:36 
GeneralRe: The reason you got a downvote was for not doing this:...memberAlbert Holguin27 Jul '12 - 11:19 
GeneralRe: The reason you got a downvote was for not doing this:...memberPIEBALDconsult27 Jul '12 - 14:12 
GeneralRe: The reason you got a downvote was for not doing this:...memberAlbert Holguin27 Jul '12 - 16:07 
GeneralRe: Correct that and I'll change my vote...memberAlbert Holguin30 Jan '12 - 4:56 
GeneralReason for my vote of 2 Although your tip is good, you commi...memberAlbert Holguin29 Jan '12 - 18:08 
GeneralRe: Reason for my vote of 2Although your tip is good, you commi...memberCodyDaemon23 Aug '12 - 1:54 
GeneralRe: Reason for my vote of 2Although your tip is good, you commi...memberAlbert Holguin23 Aug '12 - 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 4 Feb 2012
Article Copyright 2012 by Lakamraju Raghuram
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid