4. Can I See Results?

With printf()

image

If neither you nor anybody else could see your program’s output, there would be little use for your program! Ultimately, you have to be able to view the results of a program. C’s primary means for output is the printf() function. There is no actual command that performs output, but the printf() function is a part of every C compiler and one of the most-used features of the language.

What printf() Does

In a nutshell, printf() produces output on your screen. As Figure 4.1 shows, printf() sends characters, numbers, and words to the screen. There is a lot to printf(), but you don’t have to be an expert in all the printf() options (very few C programmers are) to use printf() for all your program’s screen output.

Figure 4.1. printf() sends characters, numbers, and words to the screen.

image

The Format of printf()

printf() takes many forms, but once you get used to its format, printf() is easy to use. Here is the general format of printf():

printf(controlString [, data]);

This book often shows the format of commands and functions when you first see them. The format is the general look of the statement. If something in a format appears in brackets, such as, data just shown, that part of the statement is optional. You almost never type the brackets themselves. If brackets are required in the command, that is made clear in the text following the format. printf() requires a controlString, but the data following the controlString is optional.

Warning

image

printf() doesn’t actually send output to your screen, but does send it to your computer’s standard output device. Most operating systems, including MS-DOS, route the standard output to your screen unless you know enough about MS-DOS to route the output elsewhere. Most of the time you can ignore this standard output device stuff because you’ll almost always want output to go to the screen. Other C functions you will learn about later route output to your printer and disk drives.

Warning

image

You might be wondering why some of the words in the format appear in italics. It’s because they’re placeholders. A placeholder is a name, symbol, or formula that you supply. Placeholders are italicized in the format of functions and commands to let you know that you should substitute something at that place in the command.

Here is an example of a printf():

printf("I am %d", 16);  /* Prints I am 16 */

Because every string in C must be enclosed in quotation marks (as mentioned in Chapter 2), the controlString must be in quotation marks. Anything following the controlString is optional and is determined by the values you want printed.

Note

image

Every C command and function needs a semicolon (;) after it to let C know that the line is finished. Braces and the first lines of functions don’t need semicolons because nothing is executing on those lines. All statements with printf() should end in a semicolon. You won’t see semicolons after main(), however, because you don’t explicitly tell C to execute main(); you do, however, tell C to execute print() and many other functions. As you learn more about C, you’ll learn more about semicolon placement.

Printing Strings

String messages are the easiest type of data to print with printf(). You only have to enclose the string in quotation marks. The following printf() prints a message on the screen:

printf("Read a lot");

When the computer executes this statement, the message Read a lot appears on-screen.

Note

image

The string Read a lot is the controlString in this printf(). There is little control going on here, just output.

The following two printf() statements:

printf("Read a lot");
printf("Keep learning");

might not produce the output you expect. Here is what the two printf()s produce:

Read a lotKeep learning

Clue

image

C does not automatically move the cursor down to the next line when a printf() executes. You must insert an escape sequence in the controlString if you want C to go to the next line after a printf().

Escape Sequences

C contains a lot of escape sequences, and you’ll use some of them in almost every program you write. Table 4.1 contains a list of some of the more popular escape sequences.

Table 4.1. Escape sequences.

image

Note

image

The term escape sequence sounds harder than it really is. An escape sequence is stored as a single character in C and produces the effect described in Table 4.1. When C sends 'a' to the screen, for example, the computer’s bell is sounded instead of the characters and a actually being printed.

You will see a lot of escape sequences in printf() functions. Anytime you want to “move down” to the next line when printing lines of text, you must type so that C produces a newline, which moves the blinking cursor down to the next line on the screen. The following printf() statements print their messages on separate lines because of the at the end of the first one:

printf("Read a lot ");
printf("Keep learning");

Because a quotation mark ends a string and because a backslash signals the start of an escape sequence, they have their own escape sequences. a rings your computer’s bell, and causes the output to appear moved over a few spaces. Be sure to check your compiler’s manual for other escape sequences your version of C supports. Although Table 4.1’s escape sequences are almost universal (and ANSI C compatible), not all ANSI C compilers support the escape sequences in every compiler mode. (Visual C++ does not let you use a when you compiler in the QuickWin mode, for example.)

The following printf() statements produce the output shown in their comments:

image

Clue

image

Different C compilers might produce a different number of tabbed spaces for the escape sequence.

Conversion Characters

When you print numbers and characters, you must tell C exactly how to print them. You indicate the format of numbers with conversion characters. Table 4.2 lists a few of C’s most-used conversion characters.

Table 4.2. Conversion characters.

image

When you want to print a value inside a string, insert the appropriate conversion characters in the controlString. Then, to the right of the controlString, list the value you want to be printed. Figure 4.2 is an example of how a printf() can print three numbers—an integer, a floating-point value, and another integer.

Figure 4.2. printf() conversion characters determine how and where numbers print.

image

Strings and characters have their own conversion characters as well. Although you don’t need %s to print strings by themselves, you might need %s when printing strings combined with other data. The next printf() prints a different type of data value using each of the conversion characters:

printf("%s %d %f %c ", "Sam", 14, -8.76, 'X'),

This printf() produces this output:

Sam 14 -8.760000 X

Note

image

The string Sam needs quotation marks, as do all strings, and the character X needs single quotation marks, as do all characters.

Warning

image

C is crazy when it comes to floating-point numbers. Even though the -8.76 has only two decimal places, C insists on printing six decimal places.

You can control how C prints floating-point values by placing a . between the % and the f of the floating-point conversion character. (Figure 4.2 used this decimal control.) The following printf() produces four different-looking numbers even though the same floating-point number is given:

printf("%f  %.3f  %.2f  %.1f", 4.5678, 4.5678, 4.5678, 4.5678);

C rounds the floating-point numbers to the number of decimal places specified in the %.f conversion character and produces this output:

4.567800  4.568  4.57  4.6

Clue

image

Just wait! The conversion characters will mean a lot more when you learn about variables in the next chapter.

One last thing: The printf() controlString controls exactly how your output will appear. The only reason two spaces appear between the numbers is that the constrolString has two spaces between the %fs.

Reward

image

• Use printf() if you want to print data on the screen.

• Every printf() requires a control string that determines how your data will look when printed.

• Strings are easy to print. They need no special format codes or conversion characters.

• Use escape sequences to print newlines, tabs, quotes, and backslashes, and to ring the bell.

• Use conversion characters to control how numbers will look.

Pitfalls

image

• Don’t expect C to know how to format your data automatically. You must use conversion characters.

• Don’t forget %f’s decimal control unless you want C to print six decimal places with all floating-point values.

In Review

printf() sends data to the screen. A program that can’t write to the screen is rarely useful. The programs you write must be able to communicate with the user sitting at the keyboard.

printf() requires a controlString that describes the format of the data that follows. With the controlString you can specify exactly how you want your numbers and character data printed. You also can print escape sequences, which is a fancy name for special output controls that you sometimes need, such as a newline at the end of lines of output.

Code Example

Consider the following partial program listing:

image

Code Analysis

The first printf() statement prints five data values—a character, a character string, an integer, a floating-point number, and another floating-point number. The subsequent five lines then print those values one at a time. The last floating-point value’s decimal places are specified in the last printf() to limit the number of decimal positions printed. Here is the code’s output:

image

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.100.42