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); }
Output
Who are you? Dennis
Glad to meet you, Dennis
.
Notes
* The period after the final line of output is due to the newline captured by the fgets() function and output in the printf() statement. The reason is that the newline (Enter key press) is retained when read by the fgets() function.
* 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.
Copyright © 1997-2024 by QPBC.
All rights reserved