Using "escape sequences" in printf For the Beginning C Programmer

Page content

How to print the value of a variable

In this article, we 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. Suppose the integer variable a has the value 52. The statement:

printf(“The number of students = %d\n”, a);

will print:

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, a. Thus, %d is replaced by 52, giving:

>The number of students = 52

We will explain printf and format specifications in more detail later 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,

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:

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);

Escape sequence

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 (formfeed) 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 \

References

C Programming – A Beginner’s Course

Related programming references

This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

  1. C Programming For Beginners - Part 1
  2. C Programming For Beginners - Part 2
  3. C Programming for Beginners – Part 3
  4. C Programming for Beginners – Part 4
  5. C Programming For Beginners - Part 5
  6. C Programming For Beginners - Part 6
  7. C Programming For Beginners - Data Types
  8. C Programming For Beginners - Part 8
  9. C Programming For Beginners - Part 9
  10. C Programming For Beginners - Part 10
  11. C Programming For Beginners - Part 11
  12. C Programming For Beginners - Part 12
  13. C Programming For Beginners - Part 13
  14. C Programming For Beginners - Part 14
  15. C Programming For Beginners - Integer Data Types
  16. C Programming for Beginners - Part 16
  17. C Programming For Beginners - Integer Expressions, Operators and Precedence
  18. C Programming For Beginners - Part 18
  19. C Programming For Beginners - Printing Double and Float
  20. C Programming For Beginners - Mixing double, float and int