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

Just as with the printf() function, the %s signifier indicates that scanf() should store a string value in word. Table 5.1 lists the most popular scanf() signifiers.

Table 5.1. Use these special characters to tell scanf() what kind of value is being assigned to a variable.
scanf() Signifiers
SignifierMeaning
%cSingle character
%dSigned integer
%fFloat
%sString
%uUnsigned integer

There are some tricks to using this function. For starters, when using %s to read in a string, it will only read up until the first whitespace character (a space, a tab, or a newline). In other words, it can read only a single word at a time.

Second, if the function encounters input that does not match the formatting signifier, scanf() will not work. The scanf() function returns a value, namely the number of items it read in (not the number of characters or digits). If nothing was read, it will return a value of 0 or EOF (short for end of file) or -1 (the numeric equivalent to EOF).

Third, you should definitely add the maximum field width parameter to your signifier. This value, which should match the length of the variable to which the input is assigned, dictates the maximum number of characters to be read. So, with the word example, the better syntax is

scanf ("%19s", word);

By using this, you avoid possible input buffer overruns, where C tries to read in more characters than it can handle. The number 19 is used (instead of 20) because strings include a terminating character. So a word can contain 19 typed characters plus this .

The last thing you should know about using scanf() is that the syntax differs if reading in anything other than a string. To read in an integer or other nonstring value, you must pass the address of the variable to the scanf() function by preceding the variable name with the ampersand:

scanf ("%d", &number);

You'll learn about variables and their addresses in Chapter 9, “Working with Pointers,” but for now, follow that syntax for nonstring variables.

To retrieve string input

1.
Create a new file or project in your text editor or IDE.

2.
Type the standard beginning lines of code (Script 5.2):

/* Script 5.2 - name.c */
#include <stdio.h>
int main (void) {

Script 5.2. A whole word can be read using the scanf() function.


3.
Create a character string variable:

char first_name[10];

The first_name variable will be a character string, holding up to 10 characters.

4.
Prompt for, and read in, the user's first name:

printf("Enter your first name: ");
scanf ("%9s", first_name);

First, the user is prompted. As with the previous example, a space but no newline concludes the statement so that the answer is entered immediately after the prompt.

The scanf() function then reads in up to 9 characters of the typed text, which should be a string. This value is assigned to the first_name variable.

5.
Use the submitted name in a simple sentence:

printf ("Thanks, %s.
", first_name);

To print the string, again use the %s formatter, but within the printf() function.

6.
Type two more calls to the getchar() function:

getchar();
getchar();

While using these two functions to temporarily halt execution is a nice technique, it becomes more and more unreliable as more input is read or as the user mistypes input. Adjust your use of them accordingly.

Later in this chapter you'll learn a technique for automatically discarding unnecessary input.

7.
Complete the main function:

     return 0;
}

8.
Save the file as name.c, compile, and debug as necessary.

9.
Run the application, entering different values (Figures 5.5 and 5.6) to see the results.

Figure 5.5. This simple yet responsive application reads and then prints a person's name.


Figure 5.6. If the submitted text contains a space, only those characters up to the space will be assigned to the first_name variable.


✓ Tips

  • The %c signifier will read the next character, even if it's a blank space, tab, or newline. However, if you're reading in only a single character, you're better off using getchar().

  • The scanf() function is like printf() in that it can take a variable number of arguments. You'll see an example of this later in the chapter.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset