Specifications

All conversion specifications start with a % symbol. If you actually want to print a % symbol,
you will need to use %%.
The padding_character is optional. It will be used to pad your variable to the width you have
specified. An example of this would be to add leading zeroes to a number like a counter.
The - symbol is optional. It specifies that the data in the field will be left-justified, rather than
right-justified, the default.
The width specifier tells printf() how much room (in characters) to leave for the variable to
be substituted in here.
The precision specifier should begin with a decimal point. It should contain the number of
places after the decimal point you would like displayed.
The final part of the specification is a type code. A summary of these is shown in Table 4.1.
TABLE 4.1 Conversion Specification Type Codes
Type Meaning
b Interpret as an integer and print as a binary number.
c Interpret as an integer and print as a character.
d Interpret as an integer and print as a decimal number.
f Interpret as a double and print as a floating point number.
o Interpret as an integer and print as an octal number.
s Interpret as a string and print as a string.
x Interpret as an integer and print as a hexadecimal number with lowercase letters
for the digits af.
X Interpret as an integer and print as a hexadecimal number with uppercase letters
for the digits AF.
One other note, while were on the subject, is that when printing or echoing things to the
browser, you probably have noticed that we use some special characters like \n. These are a
way of writing special characters to the output. The character \n is newline. The other main
ones you will see are the \t, or tab, and the \s, or space.
Changing the Case of a String
You can also reformat the case of a string. This is not particularly useful for our application,
but well look at some brief examples.
If we start with the subject string, $subject, which we are using for our email, we can change
its case with several functions. The effect of these functions is summarized in Table 4.2.
String Manipulation and Regular Expressions
C
HAPTER 4
4
S
TRING
M
ANIPULATION
99
06 7842 CH04 3/6/01 3:41 PM Page 99