Solution for Exercise 7-13

ex0713

#include <stdio.h>

int main()
{
    char firstname[15];
    char lastname[15];

    printf("Type your first name: ");
    scanf("%s",firstname);
    printf("Type your last name: ");
    scanf("%s",lastname);
    printf("Pleased to meet you, %s %s.\n",firstname,lastname);
    return(0);
}

Notes

* I set the input size for both firstname and lastname to 15; you could set the size to something larger if you like, as long as the input buffer — the storage space for the string — is large enough to hold anticipated input.

* The compiler automatically terminates any string input by adding the \0 character. Ensure that you set aside enough storage to account for that addition. So if you know the longest last name that can be typed is 18 characters long, set the storage size to 19 characters.

* As with Exercise 7-14, if you have a last name that consists of two words, such as Von Hindenburg, the scanf() function reads only Von as input. The solution to this problem is to use the fgets() function, as demonstrated in the book.