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

(Romina) #1

4. Your World Premiere—Putting Your Program’s Results Up on


the Screen


In This Chapter


  • Using printf()

  • Printing strings

  • Coding escape sequences

  • Using conversion characters

  • Putting it all together with a code example


If neither you nor anybody else could see your program’s output, there would be little use for your
program. Ultimately, you have to be able to view the results of a program. C’s primary means of
output is the printf() function. No actual command performs output, but the printf() function
is a part of every C compiler and one of the most used features of the language.


How to Use printf()


In a nutshell, printf() produces output on your screen. As the sample code listings in Chapters 2,
“Writing Your First C Program,” and 3 , “What Does This Do? Clarifying Your Code with
Comments,” demonstrated, printf() sends characters, numbers, and words to the screen. There is
a lot to printf(), but you don’t have to be an expert in all the printf() options (very few C
programmers are) to use it for all your programs’ screen output.


The Format of printf()


The printf() function takes many forms, but when you get used to its format, printf() is easy
to use. Here is the general format of printf():


Click here to view code image


printf(controlString [, data]);

Throughout this book, when you are introduced to a new command or function, you will see its basic
format. The format is the general look of the statement. If something in a format appears in brackets,
such as , data in the printf function just shown, that part of the statement is optional. You
almost never type the brackets themselves. If brackets are required in the command, that is made clear
in the text following the format. printf() requires a controlString, but the data following the
controlString is optional.


Warning

printf() doesn’t actually send output to your screen, but it does send it to your
computer’s standard output device. Most operating systems, including Windows, route
Free download pdf