Changes

Jump to: navigation, search

Using Objective-C Preprocessor Directives

2 bytes added, 19:45, 20 November 2009
no edit summary
== The #define Statement ==
The #define statement can be used for a variety of purposes and is probably the most flexible of preprocessor directives. Perhaps one of the most common uses is to give frequently used constant values in a program a programmer friendly name and single point of definition. For the sake of an example, lets let's say you need to frequently use the boiling temperature of water (a point of contention but for the purposes of this example we will assume it to be 99.61 degrees Celsius) throughout your Objective-C program. One option might be to simply enter the constant value wherever it is needed. For example:
<tt>double liquidTemp = 99.61 - ambientTemp;</tt>
My first ever job involved writing communications software using the C programming language (on which Objective-C is based). I inherited some code written by a former employee who loathed the C language and preferred to use another programming language (the name of which escapes me). When I looked at his C code it looked nothing like any C code I had ever seen before in my life. After about an hour of trying to understand how this could be possible (surely the compiler should have refused to compile this) I realized the other programmer had using the #define compiler directive to "modify" the syntax of the C programming language to make it look more like his preferred language. Whilst I am not suggesting that you too go to these lengths it is worth knowing that such adaptability is provided by the #define preprocessor statement.
Lets Let's begin with a simple example and write a definition that assigns the word ''MINUS'' to the minus sign (-):
<tt>#define MINUS -</tt>

Navigation menu