blog

Home / DeveloperSection / Blogs / Objective-C : Macros

Objective-C : Macros

Anonymous User3485 14-Jul-2015

Previously we learn typedef keyword that is used to create data type : 


Objective-C : Typedef

 

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.

 
Object-like Macros


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 BUFFER_SIZE 1024

defines a macro named BUFFER_SIZE as an abbreviation for the token 1024.

 

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, \

                                  2, \

                                  3

int x[] = { NUMBERS };

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

The most common visible consequence of this is surprising line numbers in error messages.

There is no restriction on what can go in a macro body provided it decomposes into valid preprocessing tokens. Parentheses need not balance, and the body need not resemble valid ObjC code. (If it does not, you may get error messages from the ObjC compiler when you use the macro.)

The ObjC preprocessor scans your program sequentially. Macro definitions take effect at the place you write them. Therefore, the following input to the ObjC preprocessor

foo = X;

#define X 4

bar = X;

produces

foo = X;

bar = 4;


When the preprocessor expands a macro name, the macro's expansion replaces the macro invocation, then the expansion is examined for more macros to expand. For example,

#define TABLESIZE BUFSIZE

#define BUFSIZE 1024

TABLESIZE

==> BUFSIZE

==> 1024

TABLESIZE is expanded first to produce BUFSIZE, then that macro is expanded to produce the final result, 1024.

 

Function-like Macros


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. For example,

#define lang_init()  c_init()

lang_init()

==> c_init()


A function-like macro is only expanded if its name appears with a pair of parentheses after it. If you write just the name, it is left alone. This can be useful when you have a function and a macro of the same name, and you wish to use the function sometimes.

extern void foo(void);

#define foo() /* optimized inline version */

...

foo();

funcptr = foo;

Here the call to foo() will use the macro, but the function pointer will get the address of the real function. If the macro were to be expanded, it would cause a syntax error.

If you put spaces between the macro name and the parentheses in the macro definition, that does not define a function-like macro, it defines an object-like macro whose expansion happens to begin with a pair of parentheses.

#define lang_init ()    c_init()

lang_init()

==> () c_init()()

The first two pairs of parentheses in this expansion come from the macro. The third is the pair that was originally after the macro invocation. Since lang_init is an object-like macro, it does not consume those parentheses.

 

Next, we will learn about : Objective-C : Type Casting


Updated 13-Mar-2018
I am a content writter !

Leave Comment

Comments

Liked By