Created
January 31, 2017 18:11
-
-
Save jaxley/d5519e247248f38d77aa341876d58997 to your computer and use it in GitHub Desktop.
C code written up to answer the question of what the isdigit() C library API actually does since the type signature takes in an *integer*. What does it think are digits? Does it check that a single character is a digit 0-9? Yes! Or does it do that for longer integers? No! I tested up to INT_MAX and it only works for single-digits.
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
#include <stdio.h> | |
#include <ctype.h> | |
#include <limits.h> | |
/* #define MAX INT_MAX | |
#define MAX 65536 */ | |
#define MAX 20 | |
int main(int argc, char **argv) { | |
printf("Hello world\n"); | |
for (int i=0; i < MAX; i++) { | |
char *isDigit = isdigit(i + '0') != 0 ? "IS" : "IS NOT"; | |
printf("[%d] %s a digit\n", i, isDigit); | |
} | |
char *base64 = "I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxjdHlwZS5oPgojaW5jbHVkZSA8bGltaXRzLmg+CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciAqKmFyZ3YpIHsKCXByaW50ZigiSGVsbG8gd29ybGRcbiIpOwoJZm9yIChpbnQgaT0wOyBpIDwgSU5UX01BWDsgaSsrKSB7CgkJY2hhciAqaXNEaWdpdCA9IGlzZGlnaXQoaSkgPT0gMCA/ICJJUyIgOiAiSVMgTk9UIjsKCQlwcmludGYoIlslZF0gJXMgYSBkaWdpdFxuIiwgaSwgaXNEaWdpdCk7Cgl9Cn0K"; | |
char *cursor = base64; | |
while (*cursor != '\0') { | |
char *isDigit = isdigit(*cursor) != 0 ? "IS" : "IS NOT"; | |
printf("[%c] %s a digit\n", *cursor, isDigit); | |
++cursor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment