Home > Articles > Programming > C/C++

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

This chapter is from the book

Displaying the Values of Variables

The printf routine is the most commonly used routine in this book. This is because it provides an easy and convenient means to display program results. Not only can simple phrases be displayed, but the values of variables and the results of computations can also be displayed. In fact, Program 3.4 uses the printf routine to display the results of adding two numbers, namely 50 and 25.

Program 3.4 Displaying Variables

#include <stdio.h>

int main (void)
{
   int sum;

   sum = 50 + 25;
   printf ("The sum of 50 and 25 is %i\n", sum);

   return 0;
}

Program 3.4 Output

The sum of 50 and 25 is 75

In Program 3.4, the first C program statement declares the variable sum to be of type integer. C requires that all program variables be declared before they are used in a program. The declaration of a variable specifies to the C compiler how a particular variable will be used by the program. This information is needed by the compiler to generate the correct instructions to store and retrieve values into and out of the variable. A variable declared as type int can only be used to hold integral values; that is, values without decimal places. Examples of integral values are 3, 5, –20, and 0. Numbers with decimal places, such as 3.14, 2.455, and 27.0, for example, are known as floating-point or real numbers.

The integer variable sum is used to store the result of the addition of the two integers 50 and 25. A blank line was intentionally left following the declaration of this variable to visually separate the variable declarations of the routine from the program statements; this is strictly a matter of style. Sometimes, the addition of a single blank line in a program can help to make the program more readable.

The program statement

sum = 50 + 25;

reads as it would in most other programming languages: 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 equal sign) in the variable sum.

The printf routine call in Program 3.4 now has two items or arguments enclosed within the parentheses. These arguments are separated by a comma. The first argument to the printf routine is always the character string to be displayed. However, along with the display of the character string, you might also frequently want to have the value of certain program variables displayed. In this case, you want to have the value of the variable sum displayed at the terminal after the characters

The sum of 50 and 25 is

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

2. Note that printf also allows you to specify %d format characters to display an integer. This book consistently uses %i throughout the remaining chapters.

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

Now try to predict the output from Program 3.5.

Program 3.5 Displaying Multiple Values

#include <stdio.h>

int main (void)
{
   int value1, value2, sum;

   value1 = 50;
   value2 = 25;
   sum = value1 + value2;
   printf ("The sum of %i and %i is %i\n", value1, value2, sum);

   return 0;
}

Program 3.5 Output

The sum of 50 and 25 is 75

The first program statement declares three variables called value1, value2, and sum all to be of type int. This statement could have equivalently been expressed using three separate declaratory statements as follows:

int value1;
int value2;
int sum;

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

The call to the printf 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 display of the characters "The sum of." Similarly, the values of value2 and sum are to be printed at the appropriate points, as indicated by the next two occurrences of the %i characters in the format string.

  • + Share This
  • 🔖 Save To Your Account