Preprocessors in Objective-C
Last Updated :
12 Dec, 2023
Preprocessors help the software experts in tasks like Reusability and Conditional compilation. This article focuses on discussing Preprocessors in Objective-C.
What is Objective-C?
The Objective-C language is a general-purpose, dynamic, and high-level programming language designed to enable object-oriented programming.
- Objective-C is a small but powerful set of extensions to the standard ANSI C language.
- These extended features to C are mostly based on Smalltalk, one of the first object-oriented programming languages.
What are Objective-C Preprocessors?
Preprocessors are the special directives provided in the Language that help in pre-processing tasks such as defining a Macro or including the header files required to perform certain programming tasks.
- The compiler executes the Preprocessors before compiling the source code so that all the necessary files or special functions that are important for compiling the programming can be processed.
- For example, in the below line of code, ‘#define’ preprocessors instruct the compiler that whenever the ‘LENGTH’ statement appears in the code, replace it with 20.
#define LENGTH 20
Types of Preprocessors in Objective-C
Below are the different types of preprocessor in Objective-C:
1. Preprocessors Directives
Preprocessor Directives are the instructions to the compiler to perform some tasks before the compilation. The various types of Preprocessor Directives are given below:
- #define: You can use this preprocessor to replace instances of a defined macro with its specified value.
- #include: Developers use this preprocessor to insert a particular header from another file into the current one.
- #undef: This preprocessor undefines or removes a previously defined macro. You can use this macro to alter or eliminate macros whenever the code evolves and requirements change
- #ifdef: It helps us to conditionally include or exclude code based on whether a specific macro is defined. Thus, you can configure the code branches according to specific features
- ifndef: This is just the reverse of #ifdef preprocessor which is used to conditionally include or exclude code based if the particular macro is not defined. Users can perform conditional code execution on the basis of the absence of a macro.
- #import: You can use this preprocessor to import a header file into your code. It ensures that the file is included only once unlike ‘#include’.
- #if: You can use this preprocessor to control which sections of code are compiled.
- #else: You can use this preprocessor to provide an alternative code path when the condition specified after the ‘#if’ is not met.
- #elif: This preprocessor combines the roles of #else and #if into a single preprocessor which makes the code concise.
- #endif: It indicates the end of a conditional block.
- #error: It displays an error message on the standard error output (stderr).
- #pragma: Developers use this preprocessor to issue standardized commands to the compiler for compiler-specific directives in their code.
2. Preprocessor Operators
The Developers can use the Preprocessor Operators in the macro definitions to enhance the functionality. Objective-C has the following Preprocessor Operators:
1. Macro Continuation Operator (\): Users generally create the macros into the single line but when their length is increased, this operator helps us to continue a macro into the multiple lines. The use of this operator is depicted below:
ObjectiveC
// File - Stringize_Operator.m
#import <Foundation/Foundation.h>
//Macro Continuation Operator
#define message_for(a, b) \
NSLog(@#a " and " #b ": Welcome to Objective-C!\n")
int main(void)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
message_for(Rahul , Sohan);
[pool drain];
return 0;
}
Output:

2. Stringize Operator (#): If you want to convert a macro parameter into the string constant, this operator is used. This operator is used with the parameterized macros that have a number of arguments. Here, the ‘#’ operator before a and b converts them into string constants. The use of this operator is shown below:
ObjectiveC
// File - Stringize_Operator.m
#import <Foundation/Foundation.h>
#define message_for(a, b) \
NSLog(@#a " and " #b ": are planets!\n")
int main(void)
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
message_for(Sun , Saturn);
[pool drain];
return 0;
}
Output:

3. Token Pasting Operator(##): You can use this operator if you want to concatenate two or more operators together. In other words, it combines two or more arguments into a single argument. Its application is demonstrated as shown below:
ObjectiveC
#import <Foundation/Foundation.h>
#define tokenpaster(n) NSLog (@"token" #n " = %d", token##n)
int main(void) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int token34 = 40;
tokenpaster(34);
[pool drain];
return 0;
}
Output:

5. The defined Operator: This operator helps us to identify whether a constant expression has been specified using the ‘#define’ directive or not. If the macro is defined using the ‘#define’ keyword, it returns true and if it is not so, it gives false. Its use is outlined below:
ObjectiveC
#include <Foundation/Foundation.h>
// Define a macro for debug mode
#if !defined DEBUG
#define DEBUG "Debugging is On"
#endif
int main() {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Here is the message: %s\n", DEBUG);
[pool drain];
return 0;
}
Output:

Conclusion
Objective-C Preprocessors are commands which instruct the compiler to execute them without the compilation of the code. They help in defining the macros in the program, including the necessary header files in the program. You have gained a clear understanding of the Preprocessors in Objective-C including their types. You can now use them while developing your OS X and iOS-based application and perform various preprocessing actions.
Similar Reads
Operators in Objective-C
The Objective-C language is developed on top of C Programming. The operator in Objective-C is the same as the C language operators. It is primarily used in developing iOS and MacOS operating systems and software applications for iOS. Operators are used to forming a mathematical expression using vari
11 min read
Properties in Objective-C
The object-oriented programming language Objective-C is largely used to create applications for Apple's macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The
5 min read
Pointers in Objective-C
In Objective-C, Pointers are the variables that hold the memory address of another variable. You must have declared the pointer variable before its use. The size of the pointer depends on the architecture of the system. The pointer variable can be defined as a char, int, float, double, or any other
5 min read
Polymorphism in Objective-C
One of the fundamental concepts in object-oriented programming is polymorphism. It enables objects to take on a variety of forms depending on their context. In Objective-C, polymorphism allows a class to have multiple behaviors depending on the type of data it is acting on. It is one of the key char
4 min read
Strings in Objective-C
Strings are a fundamental concept in programming, and they are used to represent text in many different applications. In Objective-C, a string is an object that represents a sequence of characters. A string can be created using several built-in classes and methods, which allow you to manipulate the
4 min read
Structures in Objective-C
Objective-C is an object-oriented programming language that adds small talk-style messaging to the C programming language. Follows a bottom-up programming approach. It incorporates concepts from both procedural and object-oriented programming languages. Objective-C support structures. So in this art
5 min read
Pointer to Pointer in Objective-C
Pointers in Objective-C are a powerful and essential concept for any programmer to master. Pointers allow you to manipulate data stored in memory directly and are used to store the address of a variable. Pointer-to-pointer also known as a double pointer, is a type of pointer that holds the address o
4 min read
Variables in Objective-C
A variable is a place for data storage that our programs can access or manipulate. In Objective-C, variables have a defined type that specifies their shape and layout. They also cover the gamut of values and operations that we are capable of performing. Before moving further, we understand the guide
5 min read
Pointers to Structures in Objective C
A pointer is a variable whose value is the address of another variable, e.g., stores the address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store variable addresses. It simplifies the programs and reduces their length. It is useful for
3 min read
Data Types in Objective-C
In Objective-C, each variable has an associated data type and each data type has different amounts of memory required in a system. A data type is a classification of data that tells the compiler or interpreter how the programmer intends to use the data. Or in other words, a data type represents what
8 min read