From a1bac88d7da40ae9c9363cb51a25111d35c54835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kl=C3=A9ger?= Date: Sun, 5 Oct 2025 21:56:30 +0200 Subject: [PATCH] homework-00: Comment code --- homework-00-warmup/main.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homework-00-warmup/main.c b/homework-00-warmup/main.c index 8b14087..a10c67b 100644 --- a/homework-00-warmup/main.c +++ b/homework-00-warmup/main.c @@ -29,16 +29,24 @@ int main() { printf("%s\n", PROMPT); + // Use getline for dynamic allocation ssize_t chars_read = getline(&buf, &len, stdin); errno = 0; long input = strtol(buf, &end, 10); + // Check whether a range error occured or + // No characters were read by getline + // or check that strtol didnt move a single char if(errno == ERANGE || chars_read == -1 || end == &buf[0]) { printf("%s\n", NOT_A_NUMBER); return EXIT_FAILURE; } + // getline always adds the \n to the result string + // If the ending char by strtol is not that newline + // It means the string is not a whole number + // (e.g. 12abc, 1.23) if(*end != '\n') { printf("%s\n", INVALID_NUMBER); return EXIT_FAILURE;