codescracker


objective c

Objective-C Preprocessors



« Previous Tutorial Next Tutorial »


The preprocessor in Objective-C is not a part of the Objective-C compiler, but is a separate step in the compilation process. The preprocessor in Objective-C is simply a text substitution tool and it instructs the Objective-C compiler to do the required pre-processing before the actual compilation.

All the preprocessor commands in Objective-C begins with a pound (#) symbol. Here is the list of all the important preprocessor directives present in Objective-C:

Objective-C Preprocessor Example

Here is a code fragment shows the preprocessor in Objective-C:

#define ARRAY_LENGTH 10

Here, this preprocessor directive tells the Objective-C Preprocessor to just replace instances of ARRAY_LENGTH with 10. Here is an another code fragment also shows the preprocessor directive in Objective-C:

#undef  FILE_SIZE
#define FILE_SIZE 50

Here, the above code fragment, tells the Objective-C preprocessor to undefine the existing FILE_SIZE and now re-define it as 50. Here is one more code fragment, illustrates the preprocessor in Objective-C.

#ifndef GREET
   #define GREET "Welcome to Objective-C Preprocessor"
#endif

Here, the above Objective-C code fragment, simply tells the Objective-C Preprocessor to define GREET only if GREET is not already defined. Here is an another code fragment, also demonstrates the preprocessor in Objective-C:

#ifdef DEBUG
   /* Your debugging statements here */
#endif

The above Objective-C code fragment, tells the Objective-C Preprocessor to do, process the statements enclosed if DEBUG is defined.

Objective-C Predefined Macros

The predefined macros in Objective-C shouldn't be directly modified. Here the following table lists the predefined macros in Objective-C along with their meaning:

Macro Meaning
__TIME__ The current time as a character literal in "HH:MM:SS" format
__DATE__ The current date as character literal in "MMM DD YYYY" format
__STDC__ Defined as 1 when the compiler complies with the ANSI standard
__STDC__ Defined as 1 when the compiler complies with the ANSI standard
__FILE__ This contains the current filename as string literal

Example

Here is an example program uses the above predefined macros in Objective-C:

/* Objective-C Preprocessor - Example Program */
		
#import<Foundation/Foundation.h>
int main()
{
	NSLog(@"File = %s\n", __FILE__ );
	NSLog(@"Date = %s\n", __DATE__ );
	NSLog(@"Time = %s\n", __TIME__ );
	NSLog(@"Line = %d\n", __LINE__ );
	NSLog(@"ANSI = %d\n", __STDC__ );
   
	return 0;
}

Now, when the above Objective-C program code in a file main.m is compile and executed, it will produce the following result:

2014-10-14 04:20:07.947 demo[20683] File = main.m
2014-10-14 04:20:07.947 demo[20683] Date = Oct 14 2014
2014-10-14 04:20:07.947 demo[20683] Time = 04:46:14
2014-10-14 04:20:07.947 demo[20683] Line = 9
2014-10-14 04:20:07.947 demo[20683] ANSI = 1

Objective-C Preprocessor Operators

The Objective-C Preprocessor, also offers the following operators that can help you in creating the macros in Objective-C.

Macro Continuation (\)

A macro usually must be contained on a single line. But you can use the macro continuation operator to continue a macro that is too long or that takes more than one line. Here is an example showing the use of macro continuation (/) in Objective-C:

#define  msg(a, b)  \
    NSLog(@#a " and " #b ", We are Objective-C Programmers.\n")

Stringize (#)

The stringize also called as number-sign operator ('#'), when used within a macro definition in Objective-C, converts a macro parameter into string constant. This operator may be used only in a macro which has a specified argument/parameter list. Here is an example illustrates the concept of stringiz (#) in Objective-C:

#import <Foundation/Foundation.h>

#define  msg(a, b)  \
	NSLog(@#a " and " #b ", We are Objective-C Programmers.\n")

int main(void)
{
	msg(Devraj, Alok);
	return 0;
}

When the above code is compile and executed, it will produce the following output:

2014-10-14 04:20:07.947 demo[20683] Devraj and Alok, We are Objective-C Programmers.

Token Pasting (##)

The token-pasting operator (##) within a macro definition combines the two arguments in Objective-C. It permits two separate tokens in the macro definition to be joined into a single token. Here is an example, illustrates the concept of token pasting (##) in Objective-C:

#import <Foundation/Foundation.h>

#define tokenpaster(n) NSLog (@"token" #n " = %d", token##n)

int main(void)
{
	int token34 = 40;
	
	tokenpaster(34);
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-14 04:20:07.947 demo[20683] token34 = 40

This happened, because this example results in the following actual output from the preprocessor :

NSLog (@"token34 = %d", token34);

Here, this example shows the concatenation of token##n into token34 and here we have used both stringize and token-pasting

The defined() Operator

The preprocessor defined operator in Objective-C, is simply used in constant expressions to determine if an identifier is defined using the #define preprocessor directive. If the specified identifier is defined, then the value is true (non-zero). And if the symbol is not defined, the value is false (that is zero). Here is an example illustrates the concept of defined() operator in Objective-C:

#import <Foundation/Foundation.h>

#if !defined (GREET)
   #define GREET "Welcome to Objective-C Preprocessor Tutorial."
#endif

int main(void)
{
	NSLog(@"Hello, %s\n", GREET);  
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-14 04:20:07.947 demo[20683] Hello, Welcome to Objective-C Preprocessor Tutorial.

Parametarized Macros

The ability to simulate functions using the parametarized macros in Objective-C, is one of the powerful functions of the Objective-C Preprocessor. For instance, we might have some code to square a number like this:

int square(int a) {
   return a * a;
}

Here, we can rewrite the above code using the macro in Objective-C like this:

#define square(a) ((a) * (a))

Macros with arguments must be defined using the #define preprocessor directive before they can be used.

The argument list is simply enclosed in the parentheses and must immediately follow the macro name. Spaces are not allowed between the macro name and the open parenthesis. Here is an example demonstrating the concept of parametarized macros in Objective-C:

#import <Foundation/Foundation.h>

#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main(void)
{
   NSLog(@"Maximum Number between 30 and 10 = %d\n", MAX(10, 30));
   return 0;
}

When the above code is compile and executed, it will produce the following result:

2014-10-14 04:20:07.947 demo[20683] Maximum Number between 30 and 10 = 30

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test