



Estude fácil! Tem muito documento disponível na Docsity
Ganhe pontos ajudando outros esrudantes ou compre um plano Premium
Esta página não é visível na pré-visualização
Não perca as partes importantes!




printf is the C language function to do format- ted printing. The same function is also available in PERL. This paper explains how printf works, and how to design the proper formatting specification for any occasion.
In the early days, computer programmers would write their own subroutines to read in and print out numbers. It is not terribly difficult, actually. Just allocate a character array to hold the result, divide the number by ten, keep the remainder, add x30 to it, and store it at the end of the array. Repeat the process until all the digits are found. Then print it. Too easy, right? But even though it was easy (for Einstein), it still took some effort. And what about error checking, and negative numbers? So the computer program- mers brought forth libraries of prerecorded func- tions. And it was good. Eventually the most popular of these functions were canonized into membership in the “standard” libraries. Number printing was popular enough to gain this hallowed honor. This meant that programmers did not have to reinvent the number-printing subroutine again and again. It also meant that everybody’s favorite op- tions tried to make it into the standard. Thus was printf born.
In the most simple case, printf takes one argument: a string of characters to be printed. This string is composed of characters, each of which is printed ex- actly as it appears. So printf("xyz"); would sim- ply print an x, then a y, and finally a z. This is not exactly “formatted” printing, but it is still the basis of what printf does.
To identify the start of the string, we put a double- quote (") at the front. To identify the end of the string we put another double-quote at the end. But what if we want to actually print a double-quote? We can’t exactly put a double-quote in the middle of the string because it would be mistaken for the end-of-string marker. Double-quote is a special char- acter. The normal print-what-you-see rules do not apply. Different languages take different approaches to this problem. Some require the special character to be entered twice. C uses backslash (virgule, ) as an escape character to change the meaning of the next character after it. Thus, to print a double- quote you type in backslash double-quote. To print a backslash, you must escape it by typing another backslash in front of it. The first backslash means “give the next character its alternate meaning.” The second backslash has an alternate meaning of “print a backslash.” Without a backslash, special characters have a natural special meaning. With a backslash they print as they appear. Here is a partial list.
\ escape the next character \ print a backslash " start or end of string " print a double quote ’ start or end a character constant \’ print a single quote % start a format specification % print a percent sign
On the other hand we have characters that normally print as you would expect, but when you add a back- slash, then they become special. An example is the newline character. To print an n, we simply type in an n. To print a newline, we type in a \n, thus in-
voking the alternate meaning of n, which is newline. Here is a partial list.
\a audible alert (bell) \b backspace \f form feed \n newline (linefeed) \r carriage return \t tab \v vertical tab
The real power of printf is when we are printing the contents of variables. Let’s take the format specifier %d for example. This prints a number. So, a num- ber must be provided for printing. This is done by adding another argument to the printf statement, as shown here.
int age; age = 25; printf ( "I am %d years old\n", age );
In this example, printf has two arguments. The first is a string: "I am %d years old\n". The sec- ond is an integer, age.
When printf processes its arguments, it starts printing the characters it finds in the first argument, one by one. When it finds a percent it knows it has a format specification. It goes to the next ar- gument and uses its value, printing it according to that format specification. It then returns to printing a character at a time (from the first argument). It is okay to include more than one format speci- fication in the printf string. In that case, the first format specification goes with the first additional ar- gument, second goes with second, and so forth. Here is an example:
int x = 5, y = 10; printf ( "x is %d and y is %d\n", x, y );
Every format specification starts with a percent sign and ends with a letter. The letters are chosen to have some mnemonic meaning. Here is a partial list:
%c print a single character %d print a decimal (base 10) number %e print an exponential floating-point number %f print a floating-point number %g print a general-format floating-point number %i print an integer in base 10 %o print a number in octal (base 8) %s print a string of characters %u print an unsigned decimal (base 10) number %x print a number in hexidecimal (base 16) %% print a percent sign (% also works)
To print a number in the simple way, the format specifier is simply %d. Here are some sample cases and results.
printf produces ("%d",0) 0 ("%d",-7) - ("%d",1560133635) 1560133635 ("%d",-2035065302) -
Notice that in the simple, %d way, there is no pre- determined size for the result. printf simply takes as much space as it needs.
As I mentioned above, simply printing numbers was not enough. There were special options that were de- sired. The most important was probably the width option. By saying %5d the number was guaranteed to take up five spaces (more if necessary, never less). This was very useful in printing tables because small and large numbers both took the same amount of space. Nearly all printing was monospaced in those days, which means that a w and an i both took the same amount of space. This is still common in text editors used by programmers. To print a number with a certain (minimum) width, say 5 spaces wide, the format specifier is %5d. Here are some sample cases and results. (We will use the symbol to explicitly indicate a space.)
printf produces ("%5d",0) 0 ("%5d",-7) - ("%5d",1560133635) 1560133635 ("%5d",-2035065302) -
Notice that for shorter numbers, the result is padded out with leading spaces. For excessively long
This one is a little bizarre. It is an invisible plus sign. Instead of printing a plus on positive numbers (and zero), we print a space where the sign would go. This can be useful in printing left-justified numbers where you want the minus signs to really stand out. Notice these two alternatives.
printf produces ("%+-5d",0) + ("%+-5d",-7) - ("%+-5d",1560133635) + ("%+-5d",-2035065302) -
printf produces ("% -5d",0) 0 ("% -5d",-7) - ("% -5d",1560133635) 1560133635 ("% -5d",-2035065302) -
Remember from above that the format specifier %-5d we get the following results (shown again for easier comparison).
printf produces ("%-5d",0) 0 ("%-5d",-7) - ("%-5d",1560133635) 1560133635 ("%-5d",-2035065302) -
Notice that the plus signs disappear, but the sign still takes up space at the front of the number. Notice also that we can combine several options in the same format specifier. In this case, we have combined the options plus, minus, and five, or space, minus, and five, or just minus and five.
Here is another example of combining several options at the same time. Using the format specifier % 05d or %0 5d we get the following results.
printf produces ("% 05d",0) 0000 ("% 05d",-7) - ("% 05d",1560133635) 1560133635 ("% 05d",-2035065302) -
Using the format specifier %+05d or %0+5d we get the following results.
printf produces ("%+05d",0) + ("%+05d",-7) - ("%+05d",1560133635) + ("%+05d",-2035065302) -
When we combine plus and space at the same time, the space arranges for room for a sign and the plus uses it. It is the same as if the space was not even specified. The plus takes priority over the space.
The options are also called “flags” and among them- selves they can appear in any order. Here is a partial list.
flag effect none print normally (right justify, space fill)
After the options, if any, the minimum field width can be specified.
The %s option allows us to print a string inside a string. Here is an example.
char * grade; if ( year == 11 ) grade = "junior"; printf ( "%s is a %s\n", "Fred", grade );
The left-justify flag applies to strings, but of course the zero fill, plus sign, and invisible plus sign are meaningless.
printf produces ("%5s","") ("%5s","a") a ("%5s","ab") ab ("%5s","abcdefg") abcdefg
printf produces ("%-5s","") ("%-5s","a") a ("%-5s","ab") ab ("%-5s","abcdefg") abcdefg
Floating point numbers are those like 3.1415 that have a decimal point someplace inside. This is in contrast to ordinary integers like 27 that have no decimal point. All the same flags and rules apply for floating point numbers as do for integers, but we have a few new options. The most important is a way to spec- ify how many digits appear after the decimal point. This number is called the precision of the number. For ordinary commerce, prices are often men- tioned as whole dollars or as dollars and cents (zero or two digits of precision). For gasoline, prices are mentioned as dollars, cents, and tenths of a cent (three digits of precision). Here are some exam- ples of how to print these kinds of numbers. Let e=2.718281828.
printf produces ("%.0f",e) 3 ("%.0f.",e) 3. ("%.1f",e) 2. ("%.2f",e) 2. ("%.6f",e) 2. ("%f",e) 2. ("%.7f",e) 2.
Notice that if a dot and a number are specified, the number (the precision) indicates how many places should be shown after the decimal point. Notice that if no dot and precision are specified for %f, the default is %.6f (six digits after the decimal point). Notice that if a precision of zero is specified, the decimal point also disappears. If you want it back, you must list it separately (after the %f format spec- ifier). We can specify both a width and a precision at the same time. Notice especially that 5.2 means a total width of five, with two digits after the deci- mal. It is very common and natural to think it means five digits in front of the decimal and two digits after, but that is not correct. Be careful.
printf produces ("%5.0f",e) 3 ("%5.0f.",e) 3. ("%5.1f",e) 2. ("%5.2f",e) 2. ("%5.7f",e) 2.
We can also combine precision with the flags we learned before, to specify left justification, leading zeros, plus signs, etc.
printf produces ("%5.1f",e) 2. ("%-5.1f",e) 2. ("%+5.1f",e) +2. ("%+-5.1f",e) +2. ("%05.1f",e) 002. ("%+05.1f",e) +02. ("% 05.1f",e) 02. ("%- 5.1f",e) 2.
If you are designing a printf format specifier, the first step is to decide what kind of a thing you are printing. If it is an integer, a float, a string, or a char- acter, you will make different choices about which basic format to use. The second big question is how wide your field should be. Usually this will be the size of the biggest number you ever expect to print under normal cir- cumstances. Sometimes this is controlled by the amount of space that is provided on a pre-printed form (such as a check or an invoice). Decide what you would like printed under a va- riety of circumstances. In this paper we have of- ten illustrated the results by using a small posi- tive number, a small negative number, an oversized positive number, and an oversized negative number. You should include this options as well as large (but not oversized) numbers. Design your format for the biggest number that you would normally expect to see.
The printf test includes a variety of matching prob- lems. They are designed to be tricky, and student feedback indicates that if anything, they are more tricky that expected. You can use the process of elimination to make this test very fast and accurate. As you look at a common feature in the answer line, you can rule out all those printf statements that do not have that feature. Very quickly you can narrow your options to one or two.