Solution for Exercise 12-9

ex1209

#include <stdio.h>

int main()
{
    char firstname[16];

    printf("What is your name? ");
    fgets(firstname,16,stdin);
    printf("Pleased to meet you, %s\n",firstname);
    return(0);
}

Notes

* In this code, it's the fget() function that controls input, capping out characters read at 15, plus one for the null character, \0. The array itself doesn't have any controls or what the programmer's call "bounds checking." It's entirely possible to fill the array with more characters than its limit. That's why the fgets() function is used to read text, as it properly caps input after the given number of characters.