Retrieving Whole Word Input

To read in more than a single character requires the scanf() function. This function reads what has been typed into the keyboard and converts it to a particular data type, based on the formatting code given to it (just as the printf() function prints values based on formatting codes).

To use this function to read in a word, you must first define a character string, which was also briefly mentioned in Chapter 2. For example:

char word[20];

This creates a character string called word that can be up to 20 characters in length.

Next, you would prompt the user and tell the scanf() function what type of data to expect and to which variable that data should be assigned:

 printf ("Please enter a word: "); scanf ("%s", word); ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.