Skip to content

Instantly share code, notes, and snippets.

@jaxley jaxley/digit.c
Created Jan 31, 2017

Embed
What would you like to do?
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.
#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
You can’t perform that action at this time.