O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required