Click here to Skip to main content
15,741,688 members
Articles / Programming Languages / C++
Posted 27 Jan 2012

Tagged as


9 bookmarked

Macro expansion in VC++

Rate me:
Please Sign up or sign in to vote.
4.97/5 (19 votes)
4 Feb 2012CPOL
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:


#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 (:-


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

Written By
Technical Lead
India India

Did my masters from IIT-M in Advanced Manufacturing Technology and working mainly on C++ in CAD domain from 2004 onwards.
Working on web technologies using Angular 7.0 and above, HTML5, CSS3 from 2015.

Comments and Discussions

GeneralRe: Correct that and I'll change my vote... Pin
Albert Holguin30-Jan-12 4:56
professionalAlbert Holguin30-Jan-12 4:56 
GeneralReason for my vote of 2 Although your tip is good, you commi... Pin
Albert Holguin29-Jan-12 18:08
professionalAlbert Holguin29-Jan-12 18:08 
GeneralRe: Reason for my vote of 2Although your tip is good, you commi... Pin
CodyDaemon23-Aug-12 1:54
CodyDaemon23-Aug-12 1:54 
The point of the article is to point out that the expanded macro has that bug, when looking at it in the unexpanded form it may not be possible to so easily spot the error. i.e It is a bad use case for that very reason to highlight the problem.
GeneralRe: Reason for my vote of 2Although your tip is good, you commi... Pin
Albert Holguin23-Aug-12 4:05
professionalAlbert Holguin23-Aug-12 4:05 

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

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