Solution for Exercise 7-16

ex0716

#include <stdio.h>

int main()
{
    char name[10];

    printf("Who are you? ");
    fgets(name,10,stdin);
    printf("Glad to meet you, %s.\n",name);
    return(0);
}

Notes

* The number of characters stored in the name array is 9; the 10th character is reserved for the \0 (the null character) at the end of the string.

* Likewise, the fgets() function is aware of input limitations. It reads only 9 characters before it automatically appends the \0 to the end of the string, the 10th character in the above example.

* Always type stdin when using the fgets() function to read input from standard input, i.e., the keyboard.

* The output includes the newline stored in the string (if room is available) when you press Enter. So it may look like this:

Who are you? Dennis
Glad to meet you, Dennis
.

The period after the final line is due to the newline captured by the fgets() function and output in the printf() statement.