From 6b91325623e4a8ea04e56b3c8950aea053d37495 Mon Sep 17 00:00:00 2001 From: DeOwl Date: Wed, 9 Sep 2026 16:47:55 +0300 Subject: [PATCH 2/3] Move the ask name code into its own function and file --- askname.c | 10 ++++++++++ askname.h | 1 + main.c | 8 ++------ 3 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 askname.c create mode 100644 askname.h diff --git a/askname.c b/askname.c new file mode 100644 index 0000000..60b9420 --- /dev/null +++ b/askname.c @@ -0,0 +1,10 @@ +#include +#include +void askname(char *first, char *last) +{ + printf("Enter your first name: "); + fgets(first, 255, stdin); + first[strlen(first)-1] = '\0'; /* remove the newline at the end */ + printf("Now enter your last name: "); + gets(last); /* buffer overflow? what's that? */ +} diff --git a/askname.h b/askname.h new file mode 100644 index 0000000..99b23f5 --- /dev/null +++ b/askname.h @@ -0,0 +1 @@ +void askname(char *first, char *last); diff --git a/main.c b/main.c index e3a8745..291e6b3 100644 --- a/main.c +++ b/main.c @@ -1,13 +1,9 @@ #include -#include +#include "askname.h" int main(int argc, char **argv) { char first[255], last[255]; - printf("Enter your first name: "); - fgets(first, 255, stdin); - first[strlen(first)-1] = '\0'; /* remove the newline at the end */ - printf("Now enter your last name: "); - gets(last); /* buffer overflow? what's that? */ + askname(first, last); printf("Hello, %s %s!\n", first, last); return 0; } -- 2.55.0