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;
}
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;
}
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 ...
is
functions also return a single bit set. –
Jere isdigit
but it seems like it could: jbox.dk/sanos/source/lib/ctype.c.html –
Kristopher isdigit
does not exist. There are many implementations, none of which is the "most real". –
Antons 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 .
© 2022 - 2024 — McMap. All rights reserved.
bool
type. More precisely, it has a built-in boolean type named_Bool
, and a macro definition#define bool _Bool
in the standard header<stdbool.h>
. But that was added by the 1999 ISO C standard, andisdigit()
predates it. – Absonant!!
work-around:printf("%d", !!isdigit(c));
– Afraid