Previous: 2.2.4 The Assignment Statement
Up: 2.2 Organization of C Programs - Simple Statements
Previous Page: 2.2.4 The Assignment Statement

2.2.5 Generating Output

Writing programs which declare variables and evaluate expressions would not be very useful if there were no way to communicate the results to the user. Generally this is done by printing (or writing) messages on the output.

Output of Messages

It is a good practice for a program to indicate its name or title when it is executed to identify the task which is being performed. The next statement in our program is:

printf("***Pay Calculation***\n\n");
The statement prints the program title on the terminal. This statement invokes the standard function printf() provided by every C compiler in a standard library of functions. The function printf() performs the subtask of writing information to the screen. When this statement is executed, the flow of control in the program passes to the code for printf(), and when printf() has completed whatever it has to do, control returns to this place in the program. These sequence of events is called a function call.

As can be seen in this case, a function can be called by simply using its name followed by a (possibly empty) pair of parentheses. Anything between the parentheses is called an argument and is information being sent to the function. In the above case, printf() has one argument, a string of characters surrounded by double quotes, called a format string. As we shall soon see, printf() can have more than one argument; however, the first argument of printf() must always be a format string. This printf() statement will write the following to the screen:

followed by two newlines. Note that all of the characters inside the double quotes have been printed (but not the quotes themselves), except those at the end of the string. The backslash character, `', in the string indicates an escape sequence. It signals that the next character must be interpreted in a special way. In this case, 'n' prints out a newline character, i.e. all further printing is done on the next line of output. We will encounter other escape sequences in due time. Two newline escape sequences are used here; the first completes the line where `` Calculation***'' was written, and the second leaves a blank line in the output.

Output of Data

In addition to printing fixed messages, printf() can be used to print values of expressions by passing the values as additional arguments separated by commas. We print out values of the initial data and the result with the statements:

printf("ID Number = %d\n", id_number);
     printf("Hours Worked = %f, Rate of Pay = %f\n",
               hours_worked, rate_of_pay);
     printf("Pay = %f\n", pay);
The first argument of printf() must always be a format string and may be followed by any number of addition argument expressions (in this case simple variable names). As before, all regular characters in the format string are printed until the symbol %. The % and the following character, called a conversion specification, indicate that the value of the next argument is to be printed at this position in the output. The conversion character following % determines the format to be printed. The combination %d signals that a decimal integer value is to be printed at this position. Similarly, %f indicates that a decimal floating point value is to be printed at the indicated position. (To write a % character itself, use %% in the format string). Each conversion specifier in the format string will print the value of one argument in succession.

The first printf() statement prints the value of id_number in the position where %d is located. The internal form of the value of id_number is converted to a decimal integer format and printed out. The output is:

The next printf() writes the value of hours_worked at the position of the first %f, and the value of rate_of_pay at the position of the second %f. The internal forms are converted to decimal real numbers (i.e., floating point) and printed. The output is: Observe that all regular characters in the format string, including the newline, are printed as before. Only the format conversion specification, indicated by a % followed by a conversion character d or f, is replaced by the value of the next unmatched argument. The floating point value is printed with six digits after the decimal point by default.

The final statement prints:



Previous: 2.2.4 The Assignment Statement
Up: 2.2 Organization of C Programs - Simple Statements
Previous Page: 2.2.4 The Assignment Statement

tep@wiliki.eng.hawaii.edu
Tue Aug 16 14:01:55 HST 1994