Objective-C Preprocessors

What is Objective-C Preprocessor

                   The Objective-C Preprocessor is not piece of the compiler, yet is a different venture in the aggregation process. In oversimplified terms, an Objective-C Preprocessor is simply a content substitution instrument and it trains compiler to do obliged preprocessing before genuine arrangement. We'll allude to the Objective-C Preprocessor as the OCPP. 


               All preprocessor orders start with a pound image (#). It must be the first nonblank character, and for clarity, a preprocessor mandate ought to start in first segment. Taking after area records down terrifically vital preprocessor mandates:

DirectiveDescription
#defineSubstitutes a preprocessor macro
#includeInserts a particular header from another file
#undefUndefines a preprocessor macro
#ifdefReturns true if this macro is defined
#ifndefReturns true if this macro is not defined
#ifTests if a compile time condition is true
#elseThe alternative for #if
#elif#else an #if in one statement
#endifEnds preprocessor conditional
#errorPrints error message on stderr
#pragmaIssues special commands to the compiler using a standardized method

Preprocessors Examples

Break down the accompanying illustrations to comprehend different orders.

#define MAX_ARRAY_LENGTH 20
This mandate advises the OCPP to supplant examples of MAX_ARRAY_LENGTH with 20. Use #define for constants to build comprehensibility.

#import <Foundation/Foundation.h>
#include "myheader.h"
These mandates advise the OCPP to get foundation.h from Foundation Framework and add the content to the current source record. The following line advises OCPP to get myheader.h from the neighborhood registry and add the substance to the current source document.

#undef  FILE_SIZE
#define FILE_SIZE 42
This advises the OCPP to undefine existing FILE_SIZE and characterize it as 42.

#ifndef MESSAGE
   #define MESSAGE "You wish!"
#endif
This advises the OCPP to characterize MESSAGE just if MESSAGE isn't as of now characterized.

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

This advises the OCPP to do the methodology the announcements encased if DEBUG is characterized. This is valuable on the off chance that you pass the -DDEBUG banner to gcc compiler at the time of arrangement. This will characterize DEBUG, so you can turn investigating on and off on the fly amid gathering.

Predefined Macros


ANSI C characterizes various macros. Albeit every one is accessible for your utilization in programming, the predefined macros ought not be specifically altered.

MacroDescription
__DATE__The 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.
We should attempt the accompanying illustration:

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

At the point when the above code in a document main.m is arranged and executed, it delivers the accompanying result:

2013-09-14 04:46:14.859 demo[20683] File :main.m
2013-09-14 04:46:14.859 demo[20683] Date :Sep 14 2013
2013-09-14 04:46:14.859 demo[20683] Time :04:46:14
2013-09-14 04:46:14.859 demo[20683] Line :8
2013-09-14 04:46:14.859 demo[20683] ANSI :1

Preprocessor Operators


The Objective-C preprocessor offers taking after administrators to help you in making macros:

Macro Continuation (\)

A macro generally must be contained on a solitary line. The macro continuation administrator is utilized to proceed with a macro that is too ache for a solitary line. Case in point:

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

Stringize (#)

The stringize or number-sign administrator ('#'), when utilized inside a macro definition, changes over a macro parameter into a string steady. This administrator may be utilized just as a part of a macro that has a predefined contention or parameter list. Case in point:

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

At the point when the above code is incorporated and executed, it creates the accompanying result:

2013-09-14 05:46:14.859 demo[20683] Carole and Debra: We love you!
Token Pasting (##)

The token-sticking administrator (##) inside a macro definition consolidates two contentions. It allows two different tokens in the macro definition to be joined into a solitary token. Case in point:

#import <Foundation/Foundation.h>;

#define tokenpaster(n) NSLog (@"token" #n " = %d", token##n)
int main(void)
{
int token34 = 40;
tokenpaster(34);
return 0;
}


At the point when the above code is accumulated and executed, it creates the accompanying result:

2013-09-14 05:48:14.859 demo[20683] token34 = 40

How it happened, on the grounds that this sample brings about the accompanying genuine yield from the preprocessor:

NSLog (@"token34 = %d", token34);
This case demonstrates the linking of token##n into token34 and here we have utilized both stringize and token-gluing.

The defined() Operator

The preprocessor characterized administrator is utilized as a part of consistent outflows to figure out whether an identifier is characterized utilizing #define. On the off chance that the predetermined identifier is characterized, the quality is genuine (non-zero). In the event that the image is not characterized, the worth is false (zero). The characterized administrator is indicated as takes after

#import <Foundation/Foundation.h>;

#if !defined (MESSAGE)
#define MESSAGE "You wish!"
#endif

int main(void)
{
NSLog(@"Here is the message: %s\n", MESSAGE);
return 0;
}


At the point when the above code is accumulated and executed, it delivers the accompanying result:

2013-09-14 05:48:19.859 demo[20683] Here is the message: You wish!

Parameterized Macros


One of the intense capacities of the OCPP is the capacity to reproduce capacities utilizing parameterized macros. Case in point, we may have some code to square a number as takes after:

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

We can revamp above code utilizing a macro as takes after:

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

Macros with contentions must be characterized utilizing the #define order before they can be utilized. The contention rundown is encased in brackets and must instantly take after the macro name. Spaces are not permitted between macro name and open bracket. For instance:

#import <Foundation/Foundation.h>;

#define MAX(x,y) ((x) &gt; (y) ? (x) : (y))

int main(void)
{
NSLog(@"Max between 20 and 10 is %d\n", MAX(10, 20));
return 0;
}

At the point when the above code is aggregated and executed, it creates the accompanying result:

2013-09-14 05:52:15.859 demo[20683] Max between 20 and 10 is 20


0 comments:

Post a Comment

My Instagram