Macros in c++

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

An object-like macro is a simple identifier which will be replaced by a code fragment. It is called object-like because it looks like a data object in code that uses it. They are most commonly used to give symbolic names to numeric constants. You create macros with the `#define’ directive. `#define’ is followed by the name of the macro and then the token sequence it should be an abbreviation for, which is variously referred to as the macro’s body, expansion or replacement list. For example,      #define  ABC 123   .   The macro’s body ends at the end of the `#define’ line. You may continue the definition onto multiple lines, if necessary, using backslash-newline. When the macro is expanded, however, it will all come out on one line. For example,

#define NUMBERS  1, 
     int x[] = { NUMBERS };
          ==> int x[] = { 1, 2, 3 };

If the expansion of a macro contains its own name, either directly or via intermediate macros, it is not expanded again when the expansion is examined for more macros. This prevents infinite recursion.

self-referential macrois one whose name appears in its definition.If the self-reference were considered ause of the macro, it would produce an infinitely large expansion. To prevent this, the self-reference is not considered a macro call. It is passed into the preprocessor output unchanged.Example- #define ABC (2 + ABC) Following the ordinary rules, each reference to ABC will expand into (2 + ABC); then this will be rescannedand will expand into (2 + (2 + ABC)); and so on until the computer runs out of memory.

The self-reference rule cuts this process short after one step, at (2 + ABC). Therefore, this macro definition has the possibly useful effect of causing the program to add 2 to the value of ABC whereverfoo is referred to.

Indirect self refrence rule: If a macro x expands to use a macro y, and the expansion of y refers to the macro x, that is an indirect self-reference of xx is not expanded in this case either. Thus, if we have

     #define x (4 + y)
     #define y (2 * x)

Then x and y expand as follows:

     x    ==> (4 + y)
          ==> (4 + (2 * x))

     y    ==> (2 * x)
          ==> (2 * (4 + y))

NOTE: Each macro is expanded when it appears in the definition of the other macro, but not when it indirectly appears in its own definition.

Function-like macro: You can also define macros whose use looks like a function call. These are called function-like macros. To define a function-like macro, you use the same `#define’ directive, but you put a pair of parentheses immediately after the macro name.

—-will update it soon—–

About Jasleen Kaur

Hi, I am Jasleen kaur, a Computer Science Student.
This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s