We have spent a lot of time discussing variables, which as the name indicates, change their values as the program runs. There are, however, pieces of information that do not change value. For example, the mathematical constant π never changes. We call these things constants, and there are two common ways that Objective-C programmers define constants: #define and global variables.
In Xcode, create a new Foundation Command Line Tool called Constants.
In the standard C libraries, constants are defined using the #define preprocessor directive. The math part of the standard C library is declared in the file math.h. One of the constants defined there is M_PI. Use it in main.m:
#import <Foundation/Foundation.h> int main (int argc, const char * argv[]) { @autoreleasepool { // In literal NSString, use u for arbitrary unicode chars NSLog(@"u03c0 is %f", M_PI); } return 0; }
When you build and run it, you should see:
π is 3.141593
To see the definition, Command-click on M_PI in the editor. It will take you to the following line in math.h:
#define M_PI 3.14159265358979323846264338327950288