This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Secrets of “printf” Professor Don Colton Brigham Young University Hawaii 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. 1 Background 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. 2 Simple Printing 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. 2.1 Naturally Special Characters 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....
View Full Document
This document was uploaded on 02/08/2012.
- Winter '09