---
title: "Objective-C : Preprocessor"  
description: "Objective-C Preprocessor is just a text substitution tool and it instructs compiler to do required pre-processing before actual compilation."  
author: "Tarun Kumar"  
published: 2015-07-13  
updated: 2026-05-13  
canonical: https://www.mindstick.com/articles/1821/objective-c-preprocessor  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 6 minutes  

---

# Objective-C : Preprocessor

*Previously we learn how to create unions in ObjC:* Objective-C : Unions\

\

The Objective-C Preprocessor is not part of the compiler,but is a separate step in the [compilation](https://www.mindstick.com/interview/358/which-is-the-first-level-of-compilation-in-the-dot-net-languages) process. In simplistic terms, anObjective-C Preprocessor is just a text substitution tool and it instructscompiler to do required pre-processing before actual compilation. We'll referto the Objective-C Preprocessor as the OCPP.

All preprocessor commands begin with a pound symbol (#). It mustbe the first nonblank character, and for readability, a preprocessor directiveshould begin in first column. Following section lists down all importantpreprocessor directives:

\

| ##### Directive | ##### Description |
| --- | --- |
| #define | Substitutes a preprocessor macro |
| #include | Inserts a particular header from another file |
| #undef | Undefines a preprocessor macro |
| #ifdef | Returns true if this macro is defined |
| #ifndef | Returns true if this macro is not defined |
| #if | Tests if a compile time condition is true |
| #else | The [alternative](https://www.mindstick.com/forum/2324/what-is-the-alternative-to-the-mvc) for #if |
| #elif | #else an #if in one statement |
| #endif | Ends preprocessor conditional |
| #error | Prints [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) on stderr |
| #pragma | Issues special commands to the compiler using a standardized method |

##### Preprocessors Examples

\

Analyze the following examples to understand various directives.

\

```
#define MAX_ARRAY_LENGTH 20
```

\

This directive tells the OCPP to replace instances ofMAX_ARRAY_LENGTH with 20.

\

Use #define for constants to increasereadability.

```
#import< Foundation/Foundation.h>#include "myheader.h"
```

\

These directives tell the OCPP to get [foundation](https://www.mindstick.com/blog/159/mfc-microsoft-foundation-class).h from FoundationFramework and

\

add the text to the current source file. The next line tellsOCPP to get myheader.h

\

from the local directory and add the content tothe current source file.

```
#undef  FILE_SIZE#define FILE_SIZE 42
```

\

This tells the OCPP to undefine existing FILE_SIZE and define itas 42.

\

```
#ifndef MESSAGE   #define MESSAGE"You wish!"#endif
```

\

This tells the OCPP to define MESSAGE only if MESSAGE isn'talready defined.

\

```
#ifdef DEBUG   /* Your debuggingstatements here */#endif
```

This tells the OCPP to do the process the statements enclosed ifDEBUG is defined.

\

This is useful [if you pass](https://answers.mindstick.com/qa/31668/in-c-if-you-pass-an-array-as-an-argument-to-a-function-what-actually-gets-passed) the -DDEBUG flag to gcccompiler at the time of

\

compilation. This will define DEBUG, so you can turndebugging on and off on the

\

fly during compilation.

##### Predefined Macros

\

ANSI C defines a number of macros. Although each one is availablefor your use in

[programming](https://yourviews.mindstick.com/view/88478/vibe-coding-the-rise-of-ai-assisted-programming), the predefined macros should not be directlymodified.

\

| ##### Macro | ##### Description |
| --- | --- |
| __DATE__ | The [current date](https://www.mindstick.com/forum/323/find-the-day-name-and-month-name-from-current-date) as a character literal in "MMM DD YYYY" format |
| __TIME__ | The current time as a character literal in "HH:MM:SS" format |
| __FILE__ | This contains the current filename as a string literal. |
| __LINE__ | This contains the current line number as a decimal constant. |
| __STDC__ | Defined as 1 when the compiler complies with the ANSI standard. |

##### Let's try the following example:

**\**

#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;

}

When the above code in a file main.m is compiled andexecuted, it produces the

\

following result:

2015-07-13 10:28:47.874 demo[18741] File :main.m

2015-07-13 10:28:47.875 demo[18741] Date :Jul 13 2015

2015-07-13 10:28:47.875 demo[18741] Time :10:28:47

2015-07-13 10:28:47.875 demo[18741] Line :8

2015-07-13 10:28:47.875 demo[18741] ANSI :1

\

##### Preprocessor Operators

\

The Objective-C preprocessor offers following operators to helpyou in creating

\

macros:

\

Macro Continuation (\)

\

A macro usually must be contained on a single line. The macrocontinuation

\

operator is used to continue a macro that is too long for a singleline. *For example:*

```
#define  message_for(a, b)  \    NSLog(@#a " and " #b ": We love you!\n")
```

#####

##### Stringize (#)

\

The stringize or number-sign operator ('#'), when used within amacro definition,

\

converts a macro parameter into a string constant. Thisoperator may be used only

\

in a macro that has a specified argument or parameterlist. For example:

#import< Foundation/Foundation.h>

#define message_for(a,b) \

NSLog(@#a " and" #b ": We love you!\n")

int main(void)

{

message_for(Carole,Debra);

return 0;

}

\

When the above code is compiled and executed, it produces thefollowing result:

\

\

2015-07-13 10:31:02.425 demo[20392] Carole and Debra: We loveyou!

##### Token Pasting (##)

\

The token-pasting operator (##) within a macro definition combinestwo

\

arguments. It permits [two separate](https://www.mindstick.com/forum/159375/how-to-communicate-with-two-separate-database-in-mssql-server) tokens in the macro definition to bejoined into

\

a single token. For example:

\

#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 compiled and executed, it produces thefollowing result:

2015-07-13 10:34:50.071 demo[22027] token34 = 40

\

How it happened, because this example results in the followingactual output from the preprocessor:

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

This example shows the [concatenation](https://www.mindstick.com/forum/34153/how-to-write-program-to-file-concatenation-program-in-java-i-o) of token##n into token34 andhere we have used both stringize and token-pasting.

The defined() Operator

The preprocessor defined operator is used in constantexpressions to [determine if](https://answers.mindstick.com/qa/94704/how-do-you-determine-if-google-chrome-is-the-main-reason-why-netflix-doesn-t-work) an identifier is defined using #define. If thespecified identifier is defined, the value is true (non-zero). If the symbol isnot defined, the value is false (zero).

##### The defined operator is specified asfollows:

\

#import <Foundation/Foundation.h>

#if !defined (MESSAGE)

#define MESSAGE "Youwish!"

#endif

int main(void)

{

NSLog(@"Here is themessage: %s\n", MESSAGE);

return 0;

}

When the above code is compiled and executed, it produces thefollowing result:

2015-07-13 10:41:28.997 demo[23961] Here is the message: Youwish!

## \

##### Parameterized Macros

**\**

One of the powerful functions of the OCPP is the ability tosimulate functions using

\

parameterized macros. For example, we might have somecode to square a

\

number as follows:

```
int square(int x) {       return x * x;}
```

We can rewrite above code using a macro as follows:

\

\

```
#define square(x) ((x) * (x))
```

\

Macros with arguments must be defined using the #definedirective before they can be used. The argument list is enclosed in parenthesesand must immediately follow the macro name. Spaces are not allowed betweenmacro name and open parenthesis. For example:

```
#import< Foundation/Foundation.h> #define MAX(x,y) ((x) > (y) ? (x) : (y)) int main(void){   NSLog(@"Max between 20 and 10 is %d\n", MAX(10, 20));    return 0;}
```

When the above code is compiled and executed, it produces thefollowing result:

\

\

2015-07-13 10:42:57.664 demo[24302] Max between 20 and 10 is 20

\

*\*

*Next, we will learn about:* Objective-C Log Handling\

---

Original Source: https://www.mindstick.com/articles/1821/objective-c-preprocessor

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
