Previous: 2.2.4 The Assignment Statement
Up: 2.2 Organization of C Programs - Simple Statements
Previous Page: 2.2.4 The Assignment Statement
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.
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:
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 final statement prints: