C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
FIGURE 4.2 Output from the second listing of Chapter 4.

Notice that, because of the length of the word Quantity, the second line needed two tabs to fit the
cost of the item under the Cost heading. You might not need this—you will just need to test your
code to better understand how many spaces the tab escape sequence moves your cursor. Sometimes
skipping one line isn’t enough, but luckily, you can place multiple \n characters to jump down as
many lines as you want. Finally, seeing that the % sign is a big part of conversion characters, you
cannot put one in your controlString and expect it to print. So if you need to print a percent sign
on the screen, use the %c conversion character and place it that way.


The Absolute Minimum
The programs you write must be able to communicate with the user sitting at the
keyboard. The printf() function sends data to the screen. Key points from this
chapter to remember include:


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

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

  • Use escape sequences to print newlines, tabs, quotes, and backslashes, and to beep
    the computer as well.

  • Unless you want your floating-point numbers to print to six places after the decimal
    point, use the %f conversion character’s decimal control.

Free download pdf