Solution for Exercise 20-4
#include <stdio.h> #include <stdlib.h> int main() { char *input; /* allocate memory */ input = (char *)malloc(sizeof(char)*1024); if(input==NULL) { puts("Unable to allocate buffer! Oh no!"); exit(1); } /* use the memory */ puts("Type something long and boring:"); fgets(input,1024,stdin); puts("You wrote:"); printf("\"%s\"\n",input); return(0); }
Output
Type something long and boring:
It was a dark and stormy night. Suddenly! The program crashed!
You wrote:
"It was a dark and stormy night. Suddenly! The program crashed!
"
Notes
* The fgets() function retains the newline typed to end input, which is why the final double-quote character output from the program appears on a line by itself.
Copyright © 1997-2024 by QPBC.
All rights reserved