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

(Romina) #1

18. Increasing Your Program’s Output (and Input)


In This Chapter


  • Using putchar() and getchar()

  • Dealing with the newline consideration

  • Getting a little faster with getch()


You can produce input and output in more ways than with the scanf() and printf() functions.
This chapter shows you some of C’s built-in I/O functions that you can use to control I/O. You can use
these simple functions to build powerful data-entry routines of your own.


These functions offer the primitive capability to input and output one character at a time. Of course,
you also can use the %c format specifier with scanf() and printf() for single characters;
however, the character I/O functions explained here are a little easier to use, and they provide some
capabilities that scanf() and printf() don’t offer.


putchar() and getchar()


getchar() gets a single character from the keyboard, and putchar() sends a single character to
the screen. Figure 18.1 shows you what happens when you use these functions. They work basically
the way you think they do. You can use them just about anytime you want to print or input a single
character into a variable.


FIGURE 18.1 getchar() and putchar() input and output single characters.

Tip

Always include the stdio.h header file when using this chapter’s I/O functions, just
as you do for printf() and scanf().
Free download pdf