Solution for Exercise 20-4
ex2004
#include <stdio.h> #include <stdlib.h> int main() { char *input; input = (char *)malloc(sizeof(char)*1024); if(input==NULL) { puts("Unable to allocate buffer! Oh no!"); exit(1); } puts("Type something long and boring:"); fgets(input,1023,stdin); puts("You wrote:"); printf("\"%s\"\n",input); return(0); }
Notes
* The value 1023 in the fgets() statement (Line 15) can be 1024, the same value set for the input
buffer size by the malloc() function (Line 8). That's because fgets() automatically stops reading input after size-1
bytes.
Copyright © 1997-2024 by QPBC.
All rights reserved