diff options
author | zachir <zachir@librem.one> | 2024-03-06 23:31:51 -0600 |
---|---|---|
committer | zachir <zachir@librem.one> | 2024-03-06 23:31:51 -0600 |
commit | a94711362f844d5de6b498d05f12611312e22e0d (patch) | |
tree | fbde8aea275820d82b486aa25079dbf87d37edc4 | |
parent | ea86302d44a0370264959970eb624035aeb4cef4 (diff) |
Use strtol instead of checking each digit
This makes it both faster and smaller.
-rw-r--r-- | main.c | 34 | ||||
-rw-r--r-- | main.h | 1 |
2 files changed, 17 insertions, 18 deletions
@@ -29,12 +29,13 @@ void printversion() { int main(int argc, char **argv) { bool quiet; - size_t string_length; - uint8_t digit; + bool value; + long result; - digit = 0; quiet = false; - string_length = 0; + value = false; + result = 0; + char *output; if (argc > 1) { for (size_t i = 1; i < argc; i++) { @@ -52,26 +53,23 @@ int main(int argc, char **argv) { case 'q': quiet = true; break; - default: - printhelp(); - return -1; } } + result = strtol(argv[i], &output, 10); + value = true; } - assert(strlen(argv[argc - 1]) < SIZE_MAX); - string_length = strlen(argv[argc - 1]); - digit = argv[argc - 1][string_length - 1]; - digit -= '0'; - if (quiet) { - if (digit <= 10) - return ((digit & 1) == 0) ? SUCCESS : ERR; - } else { - if ((digit & 1) == 0) { + } + if (value) { + if (result % 2 == 0) { + if (quiet) { fprintf(stdout, "even\n"); - } else { + } + return 0; + } else { + if (quiet) { fprintf(stdout, "odd\n"); } - return SUCCESS; + return 1; } } return ERR; @@ -6,6 +6,7 @@ #include <stddef.h> #include <stdint.h> #include <stdio.h> +#include <stdlib.h> #include <assert.h> #include <string.h> |