6
Format Strings

Now that you know how functions work, let’s take a closer look at the printf function that you have been using to write to the log.

The printf function accepts a string as an argument and prints it to the log. A string is a string of characters strung together like beads on a necklace. Typically, a string is text.

A literal string is text surrounded by double quotes. In the AGoodStart project from Chapter 2, you called printf() with literal string arguments:

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​t​h​e​ ​b​e​g​i​n​n​i​n​g​ ​o​f​ ​t​h​e​ ​n​o​v​e​l​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​t​ ​w​a​s​ ​t​h​e​ ​b​e​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​I​t​ ​w​a​s​ ​t​h​e​ ​w​o​r​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​)​;​

Your output looked like this:

I​t​ ​w​a​s​ ​t​h​e​ ​b​e​s​t​ ​o​f​ ​t​i​m​e​s​.​
I​t​ ​w​a​s​ ​t​h​e​ ​w​o​r​s​t​ ​o​f​ ​t​i​m​e​s​.​

You can store a literal string in a variable of type char *:

 ​ ​ ​ ​c​h​a​r​ ​*​m​y​S​t​r​i​n​g​ ​=​ ​"​H​e​r​e​ ​i​s​ ​a​ ​s​t​r​i​n​g​"​;​

This is a C string. You could have created C strings in AGoodStart and passed them to printf():

 ​ ​ ​ ​/​/​ ​W​r​i​t​e​ ​t​h​e​ ​b​e​g​i​n​n​i​n​g​ ​o​f​ ​t​h​e​ ​n​o​v​e​l​
 ​ ​ ​ ​c​h​a​r​ ​*​f​i​r​s​t​L​i​n​e​ ​=​ ​"​I​t​ ​w​a​s​ ​t​h​e​ ​b​e​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​;​
 ​ ​ ​ ​c​h​a​r​ ​*​s​e​c​o​n​d​L​i​n​e​ ​=​ ​"​I​t​ ​w​a​s​ ​t​h​e​ ​w​o​r​s​t​ ​o​f​ ​t​i​m​e​s​.​​n​"​;​

 ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​t​h​e​ ​b​e​g​i​n​n​i​n​g​ ​o​f​ ​t​h​e​ ​n​o​v​e​l​
 ​ ​ ​ ​p​r​i​n​t​f​(​f​i​r​s​t​L​i​n​e​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​s​e​c​o​n​d​L​i​n​e​)​;​

Your output would have looked exactly the same.

Using tokens

The printf function can do more than just print literal strings. You can also use printf() to create custom strings at runtime using tokens and variables.

Reopen your ClassCertificates project. In main.c, find congratulateStudent(). Within this function, you call printf() and pass it a string with three tokens and three variables as arguments.

 ​ ​ ​ ​v​o​i​d​ ​c​o​n​g​r​a​t​u​l​a​t​e​S​t​u​d​e​n​t​(​c​h​a​r​ ​*​s​t​u​d​e​n​t​,​ ​c​h​a​r​ ​*​c​o​u​r​s​e​,​ ​i​n​t​ ​n​u​m​D​a​y​s​)​
 ​ ​ ​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​f​(​"​%​s​ ​h​a​s​ ​d​o​n​e​ ​a​s​ ​m​u​c​h​ ​%​s​ ​P​r​o​g​r​a​m​m​i​n​g​ ​a​s​ ​I​ ​c​o​u​l​d​ ​f​i​t​ ​i​n​t​o​ ​%​d​ ​d​a​y​s​.​​n​"​,​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​s​t​u​d​e​n​t​,​ ​c​o​u​r​s​e​,​ ​n​u​m​D​a​y​s​)​;​
 ​ ​ ​ ​}​

When you pass a string containing one or more tokens to printf(), the string that you pass is called the format string. In this example, the format string includes three tokens: %s, %s, and %d.

When the program is run, the tokens are replaced with the values of the corresponding variable arguments. In this case, those variables are student, course, and numDays. Your output looked something like this:

L​i​z​ ​h​a​s​ ​d​o​n​e​ ​a​s​ ​m​u​c​h​ ​i​O​S​ ​P​r​o​g​r​a​m​m​i​n​g​ ​a​s​ ​I​ ​c​o​u​l​d​ ​f​i​t​ ​i​n​t​o​ ​5​ ​d​a​y​s​.​

Tokens are replaced in order in the output: the first variable replaces the first token, and so on. Thus, if you swapped student and course in the list of variables, you would see

i​O​S​ ​h​a​s​ ​d​o​n​e​ ​a​s​ ​m​u​c​h​ ​L​i​z​ ​P​r​o​g​r​a​m​m​i​n​g​ ​a​s​ ​I​ ​c​o​u​l​d​ ​f​i​t​ ​i​n​t​o​ ​5​ ​d​a​y​s​.​

On the other hand, not all tokens and variables are interchangeable. The token you choose tells printf() how the variable’s value should be formatted. The %s token tells printf() to format the value as a string. The %d tells printf() to format the value an integer. (The d stands for decimal.)

If you use the wrong token, such as using %d when the substitution is the string "Ted", printf() will try to represent "Ted" with an integer value, which will give you strange results.

There are other tokens for other types. You will learn and use several more as you continue working through this book.

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

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