Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does isdigit() return 2048 if true?

Tags:

c++

c

ctype

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;
}
like image 405
noufal Avatar asked Jun 30 '13 15:06

noufal


People also ask

What does Isdigit return in C?

The isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it's a digit else it returns 0. For example, it returns a non-zero value for '0' to '9' and zero for others.

What library is Isdigit in C?

The isdigit() function is a predefined function of the C library, which is used to check whether the character is a numeric character from 0 to 9 or not. And if the given character is a numeric number or digit, it returns a true Boolean value or non-zero; else, it returns zero or false value.

How do you check if a character is an integer in C?

The function isdigit() is used to check that character is a numeric character or not. This function is declared in “ctype. h” header file. It returns an integer value, if the argument is a digit otherwise, it returns zero.


1 Answers

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 ...
like image 143
Oliver Charlesworth Avatar answered Sep 23 '22 05:09

Oliver Charlesworth