Part 4: printf()
Last article the function printf() was discussed. The function printf() is
used to output text onto the display screen and printf() stands for "print formatted".
If printf() stands for "print formatted", where is the formatting? The formatting in the
printf() function comes when you need to display more than just string constants. If you
wanted to print a variable integer within your string you would use:
int variable_name = 15;
printf("Here is my variable: %i \n", variable_name);
In the second line notice the %i in the string. This is a place holder. The %
character tells the printf() function to expect a place holder and the i tells
it that it is going to be an integer place holder. Then the function replaces the %i with
the first listed variable, which must be an integer. In this case it would print out:
Here is my variable: 15
If you were to print more than one number, and they were of various sizes, printf() allows you to align them.
int variable1 = 15, variable2 = 123, variable3 = 267;
printf("Here is the first:\t %3i\n", variable1);
printf("Here is the next :\t %3i\n", variable2);
printf("Here is the last :\t %3i\n", variable3);
Remember, the equal symbol is called the assignment operator. It is used to assign a value to a variable.
In this example, it assigns the value of 15 to the variable named variable1. It is important not to
confuse the assignment operator with the the equality comparison operator, the double equal sign, ==.
This is a common mistake in C programming and causes many problems since it can be hard to discover.
The \t in each string constant is a control code similar to \n. It stands for tab and tells
the function to display a tab. The number between the % and the i tells the
printf() function that it should use 3 spaces for each number. If the number is less than 3 spaces,
it will insert blank spaces to fill it out. This code fragment will print out:
Here is the first : 15 Here is the next : 123 Here is the last : 267
You can now use this in a program to convert meters to feet:
/* meters2feet.c 1.0 (24/9/97) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int meters, feet;
printf("Meters to feet\n\n");
for (meters = 0; meters < 10; meters++)
{
feet = meters/3.28;
printf("%2i%8i\n", meters, feet);
}
return 0;
}
The only new thing should be the keyword for. This keyword is part of ANSI C. It
starts a loop in order to repeat the same function, or group of functions, without having to
type them over and over. The first part in the parenthesis is the starting condition. In this
case it assigns the value 0 to the variable meters. The next part is the continuing condition.
As long as the value of meters is less than 10, the loop will continue. The last part is the
counting condition. This is a shortcut you will see often in C. The ++ is known as the increment
operator. It indicates to increase the variable by 1, so meters++ is the same as saying
meters = meters + 1.
In the second printf() function, it lists %2i which is replaced by
the variable meters, and %8i, which is replaced by the value of feet.
You can have as many variables in a printf() statement as you want, just be sure to place them
in the correct order. Since feet is an integer in this example, it is not very accurate. To
correct this you have to tell printf() to put it in as a float. When you set places for
floats you also need to set the number of places after the decimal that will be displayed.
/* meters2feet.c 2.0 (24/9/97) */
#include <stdio.h>
int main(int argc, char *argv[])
{
int meters;
float feet;
printf("Meters to feet\n\n");
for (meters = 0; meters < 10; meters++)
{
feet = meters/3.28;
printf("%2i%11.2f\n", meters, feet);
}
return 0;
}
Notice how the number 8 was changed to 11.2 before the second place holder, which is now an f
instead of an i. The f tells the printf() function that it is a
float place holder now and not integer. The 11 has to take into account all numbers,
including those after the decimal place and the position of the decimal itself, and the .2
indicates to display 2 places after the decimal. This version should show a far more accurate
representation of the conversion between meters and feet.
Using the printf() function is fairly straight forward. Practice using it to display text
and variables in any manner you chose. In addition to \n and \t control codes there are others that your
C compiler will support. See your compiler's documentation for a full listing.

