Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

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. The definition of a variable specifies to the Objective-C compiler how the program should use it. The compiler needs this information to generate the correct instructions to store and retrieve values into and out of the variable. A variable defined as type int can be used to hold only integral values—that is, values without decimal places. Examples of integral values are 3, 5, –20, and 0. Numbers with decimal places, such as 2.14, 2.455, and 27.0, are known as floating-point numbers and are real numbers.

The integer variable sum stores the result of the addition of the two integers 50 and 25. We have intentionally left a blank line following the definition of this variable to visually separate the variable declarations of the routine from the program statements; this is strictly a matter of style. Sometimes adding a single blank line in a program can make the program more readable.

The program statement reads as it would in most other programming languages:

sum = 50 + 25;

The number 50 is added (as indicated by the plus sign) to the number 25, and the result is stored (as indicated by the assignment operator, the equals sign) in the variable sum.

The NSLog routine call in Program 2.4 now has two arguments enclosed within the parentheses. These arguments are separated by a comma. The first argument to the NSLog routine is always the character string to be displayed. However, along with the display of the character string, you often want to have the value of certain program variables displayed as well. In this case, you want to have the value of the variable sum displayed after these characters are displayed:

The sum of 50 and 25 is

The percent character inside the first argument is a special character recognized by the NSLog function. The character that immediately follows the percent sign specifies what type of value is to be displayed at that point. In the previous program, the NSLog routine recognizes the letter i as signifying that an integer value is to be displayed.

Whenever the NSLog routine finds the %i characters inside a character string, it automatically displays the value of the next argument to the routine. Because sum is the next argument to NSLog, its value is automatically displayed after “The sum of 50 and 25 is”.

Now try to predict the output from Program 2.5.

Program 2.5.

#import <Foundation/Foundation.h>

int main (int argc, const char *argv[])
{
   @autoreleasepool {
      int value1, value2, sum;

      value1 = 50;
      value2 = 25;
      sum = value1 + value2;

      NSLog (@"The sum of %i and %i is %i", value1, value2, sum);
   }
   return 0;
}

Program 2.5. Output

The sum of 50 and 25 is 75

The second program statement inside main defines three variables called value1, value2, and sum, all of type int. This statement could have equivalently been expressed using three separate statements, as follows:

int value1;
int value2;
int sum;

After the three variables have been defined, the program assigns the value 50 to the variable value1 and then the value 25 to value2. The sum of these two variables is then computed and the result assigned to the variable sum.

The call to the NSLog routine now contains four arguments. Once again, the first argument, commonly called the format string, describes to the system how the remaining arguments are to be displayed. The value of value1 is to be displayed immediately following the phrase “The sum of.” Similarly, the values of value2 and sum are to be printed at the points indicated by the next two occurrences of the %i characters in the format string.

  • + Share This
  • 🔖 Save To Your Account