Open In App

Preprocessors in Objective-C

Last Updated : 12 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
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.

FAQs

1. How can I create the Objective-C Program?

You can Create the Objective-C file with the desired file name. Save it with the ‘.m’ extension and then write the code as per the requirement.

2. Can Objective-C be used to develop applications for Other Platforms?

Developers primarily use Objective-C for developing Apple Applications and APIs. But, you can also use Objective-C for applications other than Apple and iOS.

3. Can I compile an Objective-C Program in Linux or Windows?

Yes, you can compile the Objective-C Programs on systems like Linux or Windows. It can be compiled in other systems that support GNU, GCC, or LLVM/Clang.

4. What is the difference between Preprocessor Directives and Macros?

The Preprocessor Directive is a statement that is used to create and manipulate macros. On the other hand, Macros are the placeholders that are substituted by Preprocessor Directives before the compilation of the code.

5. How do Preprocessors help in conditional code compilation?

You can use the preprocessors such as ‘#ifdef’, ‘#ifndef’, and ‘defined’ operators to check certain conditions before the compilation process. Thus, you can compile the code if certain conditions are satisfied.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads