Suppose we want to write a program to print the following lines from The Gitanjali by Rabindranath Tagore:
Where the mind is without fear And the head is held high
Our initial attempt might be this:
#include <stdio.h>
int main() {
printf("Where the mind is without fear");
printf("And the head is held high");
}
However, when run, this program will print:
Where the mind is without fearAnd the head is held high
Note that the two strings are joined together (we say the strings are concatenated). This happens because printf does not place output on a new line, unless this is specified explicitly.
Put another way, printf does not automatically supply a newline character after printing its argument(s). A newline character would cause subsequent output to begin at the left margin of the next line.
In the example, a newline character is not supplied after fear is printed so that And the head... is printed on the same line as fear and immediately after it.
1.7.1 The Newline Character, \n (backslash n)
To get the desired effect, we must tell printf to supply a newline character after printing ...without fear. We do this by using the character sequence \n (backslash n) as in Program P1.3.
Program P1.3
#include <stdio.h>
int main() {
printf("Where the mind is without fear\n");
printf("And the head is held high\n");
}
The first \n says to terminate the current output line; subsequent output will start at the left margin of the next line. Thus, And the... will be printed on a new line. The second \n has the effect of terminating the second line. If it were not present, the output will still come out right, but only because this is the last line of output.
A program prints all pending output just before it terminates. (This is also the reason why our first program worked without \n.)
As an embellishment, suppose we want to put a blank line between our two lines of output, like this:
Where the mind is without fear And the head is held high
Each of the following sets of statements will accomplish this:
printf("Where the mind is without fear\n\n");
printf("And the head is held high\n");
printf("Where the mind is without fear\n");
printf("\nAnd the head is held high\n");
printf("Where the mind is without fear\n");
printf("\n");
printf("And the head is held high\n");
We just have to make sure we print two \n's between fear and And. The first \n ends the first line; the second ends the second line, in effect, printing a blank line. C gives us a lot of flexibility in how we write statements to produce a desired effect.
exerCise: write a program to print the lyriCs of your favorite song
1.7.2 Escape Sequences
Within the string argument to printf, the backslash (\) signals that a special effect is needed at this point. the character following the backslash specifies what to do. this combination (\ followed by another character) is referred to as an escape sequence. the following are some escape sequences you can use in a string in a printf statement:
\n issue a newline character
\f issue a new page (form feed) character
\t issue a tab character
\" print "
\\ print \
For example, using an escape sequence is the only way to print a double quote as part of your output.
suppose we want to print the line Use " to begin and end a string if we typed
printf("Use " to begin and end a string\n");
then C would assume that the double quote after Use ends the string (causing a subsequent error when it can’t figure out what to do with to). Using the escape sequence \", we can correctly print the line with:
printf("Use \" to begin and end a string\n");
exerCise: write a statement to print the line:
an esCape sequenCe starts with \
1.7.3 Print the Value of a Variable
so far, we have used printf to print the value of a string constant (that is, the characters of the string excluding the quotes). We now show how we can print the value of a variable ignoring, for the moment, how the variable gets its value. (We will see how in Chapter 2.) suppose the integer variable m has the value 52. the statement:
printf("The number of students = %d\n", m);
will print this:
The number of students = 52
this printf is a bit different from those we have seen so far. this one has two arguments—a string and a variable. the string, called the format string, contains a format specification %d. (in our previous examples, the format string contained no format specifications.) the effect, in this case, is that the format string is printed as before, except that the %d is replaced by the value of the second argument, m.
thus, %d is replaced by 52, giving this:
The number of students = 52
We will explain printf and format specifications in more detail in Chapter 2, but, for now, note that we use the specification %d if we want to print an integer value.
What if we want to print more than one value? this can be done provided that each value has a corresponding format specification. For example, suppose that a has the value 14 and b has the value 25.
Consider the statement:
printf("The sum of %d and %d is %d\n", a, b, a + b);
this printf has four arguments—the format string and three values to be printed: a, b, and a+b.
the format string must contain three format specifications: the first will correspond to a, the second to b, and the third to a+b. When the format string is printed, each %d will be replaced by the value of its corresponding argument, giving this:
The sum of 14 and 25 is 39
exerCise: what is printed by the following statement?
printf(“%d + %d = %d\n”, a, b, a + b);