Example 1
#include <stdio.h>
#include <string.h>
int main(void) {
char name[32] = "Guest";
printf("Hello, %s\n", name);
puts("Enter a short word:");
if (fgets(name, sizeof name, stdin) != NULL) {
name[strcspn(name, "\n")] = '\0'; /* strip newline */
printf("You typed: %s\n", name);
}
return 0;
} How It Works
printfprints the currentname.fgetsreads up tosizeof name - 1chars and adds\0.strcspnfinds the newline so we can replace it with\0.
