Why isdigit () returns 2048 if true?

Can someone explain why isdigit returns 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; } 
+4
source share
2 answers

Because it is allowed. The C99 standard only talks about isdigit , isalpha , etc.:

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

As for what happens in practice, I'm not sure. By assumption, it uses a lookup table shared by all is* functions and masks everything except for a specific bit position. eg:.

 static const int table[256] = { ... }; // ... etc ... int isalpha(char c) { return table[c] & 1024; } int isdigit(char c) { return table[c] & 2048; } // ... etc ... 
+22
source

Since there is no standard document for determining how to display bool by the specified number, for the C language it is nonzero true and zero is false. therefore, it depends on the actual implementation.

0
source

Source: https://habr.com/ru/post/1488965/


All Articles