Displaying the Values of Variables

Not only can simple phrases be displayed with NSLog, but the values of variables and the results of computations can be displayed as well. Program 2.4 uses the NSLog routine to display the results of adding two numbers, 50 and 25.

Program 2.4

#import <Foundation/Foundation.h>int main (int argc, const char *argv[]){   @autoreleasepool {      int sum;      sum = 50 + 25;      NSLog (@"The sum of 50 and 25 is %i", sum);   }   return 0;}

Program 2.4 Output

The sum of 50 and 25 is 75

The first program statement inside main after the autorelease pool is set up defines the variable sum to be of type integer. You must define all program variables before you can use them in a program. ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.