08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 1 ■ Elementary Programming Concepts

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

16

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!