Click here to Skip to main content
11,435,319 members (51,678 online)
Click here to Skip to main content

Tagged as

Macro expansion in VC++

, 4 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Expanding a macro in VC++
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)

Share

About the Author

Lakamraju Raghuram
Software Developer
India India
_____________________________________________________________

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.

Comments and Discussions

 
GeneralMy vote of 3 Pin
rahman_tanzilur0113-Jan-13 12:00
memberrahman_tanzilur0113-Jan-13 12:00 
QuestionVS addin [modified] Pin
sokolov766-Aug-12 0:04
membersokolov766-Aug-12 0:04 
AnswerRe: VS addin Pin
Lakamraju Raghuram6-Aug-12 2:51
memberLakamraju Raghuram6-Aug-12 2:51 
GeneralRe: VS addin Pin
Carlos Bohaa Jaa29-Aug-14 2:12
memberCarlos Bohaa Jaa29-Aug-14 2:12 
GeneralRe: VS addin Pin
sokolov7614-Apr-15 4:20
membersokolov7614-Apr-15 4:20 
GeneralMy vote of 5 Pin
Volynsky Alex19-May-12 2:33
memberVolynsky Alex19-May-12 2:33 
GeneralRe: This way of analysis is cool .... how many will scroll thoug... Pin
Lakamraju Raghuram31-Jan-12 8:03
memberLakamraju Raghuram31-Jan-12 8:03 
GeneralRe: Technically you should wrap the usage of macro arguments wit... Pin
Chuck O'Toole4-Feb-12 17:22
memberChuck O'Toole4-Feb-12 17:22 
GeneralReason for my vote of 5 Good tip Pin
Manish K. Agarwal20-Feb-12 1:12
memberManish K. Agarwal20-Feb-12 1:12 
GeneralReason for my vote of 5 Very well explained Pin
Joxemi7-Feb-12 5:42
memberJoxemi7-Feb-12 5:42 
GeneralReason for my vote of 5 Helped resolve 3rd party library API... Pin
mriddle6-Feb-12 14:06
membermriddle6-Feb-12 14:06 
GeneralIt would add to the article to describe why the result is 16... Pin
nv33-Feb-12 3:41
membernv33-Feb-12 3:41 
General(:- Pin
Lakamraju Raghuram31-Jan-12 8:02
memberLakamraju Raghuram31-Jan-12 8:02 
GeneralReason for my vote of 5 A nice tip for "What to care of..." Pin
Jose David Pujo31-Jan-12 5:59
memberJose David Pujo31-Jan-12 5:59 
GeneralHa! got you buddy.... It is copy-paste problem, I just chec... Pin
Lakamraju Raghuram30-Jan-12 9:26
memberLakamraju Raghuram30-Jan-12 9:26 
GeneralRe: +5 Pin
Albert Holguin30-Jan-12 10:33
memberAlbert Holguin30-Jan-12 10:33 
GeneralRe: At first glance, I was sure that the result of "s" should be... Pin
Jose David Pujo31-Jan-12 5:57
memberJose David Pujo31-Jan-12 5:57 
GeneralI know what I am doing. I intended to show an example where ... Pin
Lakamraju Raghuram29-Jan-12 20:07
memberLakamraju Raghuram29-Jan-12 20:07 
GeneralRe: The reason you got a downvote was for not doing this: <code>... Pin
Albert Holguin30-Jan-12 5:53
memberAlbert Holguin30-Jan-12 5:53 
GeneralRe: The reason you got a downvote was for not doing this:... Pin
PIEBALDconsult27-Jul-12 11:36
memberPIEBALDconsult27-Jul-12 11:36 
GeneralRe: The reason you got a downvote was for not doing this:... Pin
Albert Holguin27-Jul-12 12:19
memberAlbert Holguin27-Jul-12 12:19 
GeneralRe: The reason you got a downvote was for not doing this:... Pin
PIEBALDconsult27-Jul-12 15:12
memberPIEBALDconsult27-Jul-12 15:12 
GeneralRe: The reason you got a downvote was for not doing this:... Pin
Albert Holguin27-Jul-12 17:07
memberAlbert Holguin27-Jul-12 17:07 
GeneralRe: Correct that and I'll change my vote... Pin
Albert Holguin30-Jan-12 5:56
memberAlbert Holguin30-Jan-12 5:56 
GeneralReason for my vote of 2 Although your tip is good, you commi... Pin
Albert Holguin29-Jan-12 19:08
memberAlbert Holguin29-Jan-12 19:08 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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