[go: up one dir, main page]

0% found this document useful (0 votes)
46 views6 pages

Secrets of "Printf": 1 Background

This document explains the basics of printf formatting in C language. It discusses: - How printf takes a format string and additional arguments to print values according to format specifiers like %d for integers. - Special characters in strings that need to be escaped like double quotes and how backslashes are used. - Common format specifiers like %d, %s, %c for different data types. - Options for width (%5d) and left justification (%-5d) of numeric output.

Uploaded by

vicky kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views6 pages

Secrets of "Printf": 1 Background

This document explains the basics of printf formatting in C language. It discusses: - How printf takes a format string and additional arguments to print values according to format specifiers like %d for integers. - Special characters in strings that need to be escaped like double quotes and how backslashes are used. - Common format specifiers like %d, %s, %c for different data types. - Options for width (%5d) and left justification (%-5d) of numeric output.

Uploaded by

vicky kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Secrets of “printf”

Professor Don Colton


Brigham Young University Hawaii

printf is the C language function to do format-ted 2.1 Naturally Special Characters


printing. The same function is also available in
To identify the start of the string, we put a double-
PERL. This paper explains how printf works, and
quote (") at the front. To identify the end of the
how to design the proper formatting specification
string we put another double-quote at the end. But
for any occasion.
what if we want to actually print a double-quote?
We can’t exactly put a double-quote in the middle
1 Background of the string because it would be mistaken for the
end-of-string marker. Double-quote is a special
In the early days, computer programmers would char-acter. The normal print-what-you-see rules do
write their own subroutines to read in and print out not apply.
numbers. It is not terribly difficult, actually. Just Different languages take different approaches
allocate a character array to hold the result, divide to this problem. Some require the special
the number by ten, keep the remainder, add x30 to character to be entered twice. C uses backslash
it, and store it at the end of the array. Repeat the (virgule, \) as an escape character to change the
process until all the digits are found. Then print it. meaning of the next character after it. Thus, to
Too easy, right? print a double-quote you type in backslash double-
But even though it was easy (for Einstein), it still quote. To print a backslash, you must escape it by
took some effort. And what about error checking, and typing another backslash in front of it. The first
negative numbers? So the computer program-mers backslash means “give the next character its
brought forth libraries of prerecorded func-tions. And alternate meaning.” The second backslash has an
it was good. Eventually the most popular of these alternate meaning of “print a backslash.”
functions were canonized into membership in the Without a backslash, special characters have a
“standard” libraries. Number printing was popular natural special meaning. With a backslash they
enough to gain this hallowed honor. print as they appear. Here is a partial list.
This meant that programmers did not have to
reinvent the number-printing subroutine again and \ escape the next character
again. It also meant that everybody’s favorite op- \\ print a backslash
tions tried to make it into the standard. " start or end of string
Thus was printf born. \" print a double quote
’ start or end a character constant
\’ print a single quote
2 Simple Printing % start a format specification
\% print a percent sign
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 2.2 Alternately Special Characters
printed ex-actly as it appears. So printf("xyz"); On the other hand we have characters that normally
would sim-ply print an x, then a y, and finally a z. print as you would expect, but when you add a back-
This is not exactly “formatted” printing, but it is still slash, then they become special. An example is the
the basis of what printf does. 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. %c print a single character
%d print a decimal (base 10) number
\a audible alert (bell) %e print an exponential floating-point number
\b backspace %f print a floating-point number
\f form feed %g print a general-format floating-point number
\n newline (linefeed) %i print an integer in base 10
\r carriage return %o print a number in octal (base 8)
\t tab %s print a string of characters
\v vertical tab %u print an unsigned decimal (base 10) number
%x print a number in hexidecimal (base 16)
% print a percent sign (\% also works)
3 Format Specifications
To print a number in the simple way, the format
The real power of printf is when we are printing the
specifier is simply %d. Here are some sample
contents of variables. Let’s take the format
cases and results.
specifier %d for example. This prints a number.
So, a num-ber must be provided for printing. This printf produces
is done by adding another argument to the printf ("%d",0) 0
statement, as shown here. ("%d",-7) -7 ("%d",1560133635)
1560133635 ("%d",-2035065302)
int age; -2035065302
age = 25;
printf ( "I am %d years old\n", age ); Notice that in the simple, %d way, there is no
pre-determined size for the result. printf simply
In this example, printf has two arguments. The takes as much space as it needs.
first is a string: "I am %d years old\n". The sec-ond
is an integer, age.
3.3 The Width Option
3.1 The Argument List As I mentioned above, simply printing numbers was
not enough. There were special options that were de-
When printf processes its arguments, it starts sired. The most important was probably the width
printing the characters it finds in the first argument, option. By saying %5d the number was guaranteed
one by one. When it finds a percent it knows it has to take up five spaces (more if necessary, never
a format specification. It goes to the next ar- less). This was very useful in printing tables because
gument and uses its value, printing it according to small and large numbers both took the same amount
that format specification. It then returns to printing of space. Nearly all printing was monospaced in
a character at a time (from the first argument). those days, which means that a w and an i both took
It is okay to include more than one format speci- the same amount of space. This is still common in
fication in the printf string. In that case, the first text editors used by programmers.
format specification goes with the first additional To print a number with a certain (minimum) width,
ar-gument, second goes with second, and so forth. say 5 spaces wide, the format specifier is %5d. Here
Here is an example: are some sample cases and results. (We will use the
symbol to explicitly indicate a space.)
int x = 5, y = 10;
printf ( "x is %d and y is %d\n", x, y ); printf produces
("%5d",0) 0
3.2 Percent ("%5d",-7) -7
("%5d",1560133635) 1560133635
Every format specification starts with a percent sign
("%5d",-2035065302) -2035065302
and ends with a letter. The letters are chosen to have
some mnemonic meaning. Here is a partial list: Notice that for shorter numbers, the result is
padded out with leading spaces. For excessively long
numbers there is no padding, and the full number printf produces
is printed. ("%-5d",0) 0
In normal usage, one would make the field wide ("%-5d",-7) -7 ("%-5d",1560133635)
enough for the biggest number one would ever ex- 1560133635 ("%-5d",-2035065302)
pect. If your numbers are usually one, two, or -2035065302
three digits long, then %3d is probably adequate.
As before, for shorter numbers, the result is
In ab-normal usage, one could end up printing a
padded out with spaces. For longer numbers there
number that is too big for the field. printf makes the
is no padding, and the number is not shortened.
deci-sion to print such numbers fully, even though
they take too much space. This is because it is
better to print the right answer and look ugly than 3.6 The Zero-Fill Option
to print the wrong answer and look pretty. To make things line up nice and pretty, it is
common to write a date using leading zeros. We
3.4 Filling the Extra Space can write May 5, 2003 in the US as 05/05/2003.
We could also write it as 2003.05.05. Notice that in
When printing a small number like 27 in a %5d both cases, the leading zeros do not change the
field, the question then became where to put the meaning. They just make it line up nicely in lists.
27 and what to put in the other three slots. It could When a number is zero-filled, the zeros always
be printed in the first two spaces, the last two go in front, and the resulting number is both left-
spaces, or maybe the middle two spaces (if that
and right-justified. In this case the minus sign has
can be deter-mined). The empty spaces could be
no effect. To print a zero-filled number 5 spaces
filled with the blank character, or perhaps stars
wide the format specifier is %05d. Here are some
(***27 or 27*** or **27*), or dollar signs ($$$27), or
sample cases and results.
equal signs (===27), or leading zeros (like 00027).
These extra characters are often called “check printf produces
pro-tection” characters because the are intended ("%05d",0) 00000
to pre-vent bad guys from changing the dollar ("%05d",-7) -0007
amount on a printed check. It is relatively easy to ("%05d",1560133635) 1560133635
change a space into something else. It is harder to ("%05d",-2035065302) -2035065302
change a star, a dollar sign, or an equal sign.
Shorter numbers are padded out with leading
printf provides space fill (left or right) and zero fill
ze-ros. Longer numbers are unchanged.
(left only). If you want check protection or cen-
tering you need to make other arrangements. But
even without check protection or centering printf 3.7 Fun With Plus Signs
still has an impressive (and bewildering) collection Negative numbers always print with a minus sign.
of options. Positive numbers and zero usually do not print
with a sign, but you can request one. A plus (+) in
3.5 The Justify Option the format specifier makes that request.
To print a signed number 5 spaces wide the
Using printf numbers can be left-justified (printed
format specifier is %+5d. Here are some sample
in the left side of the field) or right-justified (printed
cases and results.
in the right side of the field). The most natural way
to print numbers seems to be right-justified with printf produces
leading spaces. That is what %5d means: print a ("%+5d",0) +0
base-10 number in a field of width 5, with the num- ("%+5d",-7) -7 ("%+5d",1560133635)
ber right-aligned and front-filled with spaces. +1560133635 ("%+5d",-2035065302)
To make the number left-aligned, a minus sign is -2035065302
added to the format specifier. To print a number 5
Notice that zero is treated as a positive number.
spaces wide and left-justified (left-aligned) the for-
Shorter numbers are padded. Longer numbers are
mat specifier is %-5d. Here are some sample
unchanged.
cases and results. Plus and minus are not related. Both can appear
in a format specifier.
3.8 The Invisible Plus Sign printf produces
("%+05d",0) +0000
This one is a little bizarre. It is an invisible plus
("%+05d",-7) -0007 ("%
sign. Instead of printing a plus on positive
+05d",1560133635) +1560133635 ("%
numbers (and zero), we print a space where the +05d",-2035065302) -2035065302
sign would go. This can be useful in printing left-
justified numbers where you want the minus signs When we combine plus and space at the same
to really stand out. Notice these two alternatives. time, the space arranges for room for a sign and
the plus uses it. It is the same as if the space was
printf produces
("%+-5d",0) +0 not even specified. The plus takes priority over the
("%+-5d",-7) -7 space.
("%+-5d",1560133635) +1560133635
("%+-5d",-2035065302) -2035065302 3.10 Summary
The options are also called “flags” and among
printf produces them-selves they can appear in any order. Here is
("% -5d",0) 0
a partial list.
("% -5d",-7) -7
("% -5d",1560133635) 1560133635 flag effect
("% -5d",-2035065302) -2035065302 none print normally (right justify, space fill)
- left justify
Remember from above that the format specifier 0 leading zero fill
%-5d we get the following results (shown again for + print plus on positive numbers
easier comparison). invisible plus sign
printf produces After the options, if any, the minimum field width
("%-5d",0) 0 can be specified.
("%-5d",-7) -7 ("%-5d",1560133635)
1560133635 ("%-5d",-2035065302)
-2035065302 4 Printing Strings
Notice that the plus signs disappear, but the sign The %s option allows us to print a string inside a
still takes up space at the front of the number. string. Here is an example.
Notice also that we can combine several options
in the same format specifier. In this case, we have char * grade;
combined the options plus, minus, and five, or if ( year == 11 ) grade = "junior"; printf ( "%s is a
space, minus, and five, or just minus and five. %s\n", "Fred", grade );

The left-justify flag applies to strings, but of


3.9 Plus, Space, and Zero course the zero fill, plus sign, and invisible plus
Here is another example of combining several sign are meaningless.
options at the same time.
printf produces
Using the format specifier % 05d or %0 5d we
("%5s","")
get the following results.
("%5s","a") a
printf produces ("%5s","ab") ab
("% 05d",0) 0000 ("%5s","abcdefg") abcdefg
("% 05d",-7) -0007
("% 05d",1560133635) 1560133635 printf produces
("% 05d",-2035065302) -2035065302 ("%-5s","")
("%-5s","a") a
Using the format specifier %+05d or %0+5d we ("%-5s","ab") ab ("%-
get the following results. 5s","abcdefg") abcdefg
5 Floating Point We can also combine precision with the flags we
learned before, to specify left justification, leading
Floating point numbers are those like 3.1415 that zeros, plus signs, etc.
have a decimal point someplace inside. This is in
contrast to ordinary integers like 27 that have no printf produces
decimal point. ("%5.1f",e) 2.7
All the same flags and rules apply for floating point ("%-5.1f",e) 2.7
numbers as do for integers, but we have a few new ("%+5.1f",e) +2.7
options. The most important is a way to spec-ify how ("%+-5.1f",e) +2.7
many digits appear after the decimal point. This ("%05.1f",e) 002.7
number is called the precision of the number. ("%+05.1f",e) +02.7
For ordinary commerce, prices are often men- ("% 05.1f",e) 02.7
tioned as whole dollars or as dollars and cents ("%- 5.1f",e) 2.7
(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 6 Designing The Perfect Spec
exam-ples of how to print these kinds of numbers. If you are designing a printf format specifier, the
Let e=2.718281828. first step is to decide what kind of a thing you are
printf produces printing. If it is an integer, a float, a string, or a
("%.0f",e) 3 char-acter, you will make different choices about
("%.0f.",e) 3. which basic format to use.
("%.1f",e) 2.7 The second big question is how wide your field
("%.2f",e) 2.72 should be. Usually this will be the size of the biggest
("%.6f",e) 2.718282 number you ever expect to print under normal cir-
("%f",e) 2.718282 cumstances. Sometimes this is controlled by the
amount of space that is provided on a pre-printed
("%.7f",e) 2.7182818
form (such as a check or an invoice).
Notice that if a dot and a number are specified, Decide what you would like printed under a va-
the number (the precision) indicates how many riety of circumstances. In this paper we have of-
places should be shown after the decimal point. ten illustrated the results by using a small posi-tive
Notice that if no dot and precision are specified number, a small negative number, an oversized
for %f, the default is %.6f (six digits after the positive number, and an oversized negative
decimal point). number. You should include this options as well as
Notice that if a precision of zero is specified, the large (but not oversized) numbers. Design your
decimal point also disappears. If you want it back, format for the biggest number that you would
you must list it separately (after the %f format normally expect to see.
spec-ifier).
We can specify both a width and a precision at
the same time. Notice especially that 5.2 means a 7 Hints for the Test
total width of five, with two digits after the deci-
The printf test includes a variety of matching prob-
mal. It is very common and natural to think it
lems. They are designed to be tricky, and student
means five digits in front of the decimal and two
feedback indicates that if anything, they are more
digits after, but that is not correct. Be careful.
tricky that expected.
You can use the process of elimination to make
printf produces this test very fast and accurate. As you look at a
("%5.0f",e) 3 common feature in the answer line, you can rule
("%5.0f.",e) 3. out all those printf statements that do not have that
("%5.1f",e) 2.7 feature. Very quickly you can narrow your options
("%5.2f",e) 2.72 to one or two.
("%5.7f",e) 2.7182818
7.1 Easy Features 8 Conclusion
It is easy to see if the short numbers have leading The printf function is a powerful device for print-ing
zeros. If so, there must be a zero in the formatting numbers and other things stored in variables. With
specification. this power there is a certain amount of com-plexity.
It is easy to see if the positive numbers have Taken all at once, the complexity makes printf
plus signs. If so, there must be a plus in the seem almost impossible to understand. But the
formatting specification. complexity can be easily unfolded into simple
features, including width, precision, signage, justi-
7.2 Before, Between, and Behind fication, and fill. By recognizing and understand-
ing these features, printf will become a useful and
The next thing to watch for is the before, between,
friendly servant in your printing endeavors.
and behind of the number that is printed. In a for-
matting specification like x%5dz there is an x
before the number and a z behind the number.
The x and z are not part of the format
specification, but they are part of the printed result.
Everything else that prints is “between.”
To decide whether there is anything before or be-
hind a number, look at the oversized negative num-
ber. Any spaces before it are surely before the for-
mat specification. Any spaces behind it are surely
behind the format specification. Here is an example.
If -2035065302 prints as -2035065302 , you can
be sure that the printf string is %... , with two
spaces before the formatting specification and one
space behind. That is because all the print
positions between (the % and whatever goes with
it) are used up by the oversized number.
Once you have determined what is before and
be-hind, you can use that information to match
against the matching choices. Often this will give
you the answer directly.

7.3 The Invisible Plus Sign


Compare the oversized negative number to the
over-sized positive number. If the positive number
has an extra space in front, it is an invisible plus
sign. If there is no extra space, there is no invisible
plus sign.

7.4 Left Justification


Subtract the before and behind. Look at what is
left. Look at the small negative number. Where are
the extra spaces printed? If the are in front, the
number is right justified. If they are in back, the
number is left justified. If they are in front and in
back, you did something wrong.

You might also like