Created
June 18, 2025 11:36
-
-
Save wilyJ80/87b2814f54c764ba4dc6b8466adeb589 to your computer and use it in GitHub Desktop.
beginners guide away from scanf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* https://web.archive.org/web/20250417094758/https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <errno.h> | |
int main(void) | |
{ | |
long a; | |
char buf[1024]; // use 1KiB just to be sure | |
int success; // flag for successful conversion | |
do | |
{ | |
printf("enter a number: "); | |
if (!fgets(buf, 1024, stdin)) | |
{ | |
// reading input failed: | |
return 1; | |
} | |
// have some input, convert it to integer: | |
char *endptr; | |
errno = 0; // reset error number | |
a = strtol(buf, &endptr, 10); | |
if (errno == ERANGE) | |
{ | |
printf("Sorry, this number is too small or too large.\n"); | |
success = 0; | |
} | |
else if (endptr == buf) | |
{ | |
// no character was read | |
success = 0; | |
} | |
else if (*endptr && *endptr != '\n') | |
{ | |
// *endptr is neither end of string nor newline, | |
// so we didn't convert the *whole* input | |
success = 0; | |
} | |
else | |
{ | |
success = 1; | |
} | |
} while (!success); // repeat until we got a valid number | |
printf("You entered %ld.\n", a); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment