Open In App

Preprocessors in Objective-C

Last Updated : 12 Dec, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

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:

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:

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:

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.


Next Article
Article Tags :

Similar Reads