Question

Can anybody explain why does isdigit return 2048 if true? I am new to ctype.h library.

#include <stdio.h>
#include <ctype.h>
int main() {
  char c = '9';
  printf ("%d", isdigit(c));
  return 0;
}
Was it helpful?

Solution

Because it's allowed to. The C99 standard says only this about isdigit, isalpha, etc:

The functions in this subclause return nonzero (true) if and only if the value of the argument c conforms to that in the description of the function.

As to why that's happening in practice, I'm not sure. At a guess, it's using a lookup table shared with all the is* functions, and masking out all but a particular bit position. e.g.:

static const int table[256] = { ... };

// ... etc ...
int isalpha(char c) { return table[c] & 1024; }
int isdigit(char c) { return table[c] & 2048; }
// ... etc ...

OTHER TIPS

Because there is no standard document to define how to represented bool by specified number, and for C language, non-zero is true and zero is false. so it depends on actual implementation .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top