# why 'isdigit' always return zero ?

This is a discussion on why 'isdigit' always return zero ? within the C Programming forums, part of the General Programming Boards category; Code: int c=3; char c2='t'; int f1,f2; f1=isdigit(c); printf("%d",f1); // output is 0 f2=isdigit(c2); printf("%d",f2); // output is 0 why ...

1. ## why 'isdigit' always return zero ?

Code:
```int c=3;
char c2='t';
int f1,f2;

f1=isdigit(c);
printf("%d",f1);
// output is 0

f2=isdigit(c2);
printf("%d",f2);
// output is 0```
why 'isdigit' always return zero ?

I thought, I will find something different in 'f2' ?

I'm using C, not C++.

2. Well the short answer is that the integer 1 is not the same as the character '1' and therefore isdigit() will return zero. This is because the character '1' is actually equal to 49 (0x31) when cast to an int. See this link for isdigit() and from that link:
Parameters
c
Character to be checked, casted to an int, or EOF.
Jim

3. Originally Posted by jimblumberg
Well the short answer is that the integer 1 is not the same as the character '1' and therefore isdigit() will return zero. This is because the character '1' is actually equal to 49 (0x31) when cast to an int. See this link for isdigit() and from that link:

Jim
Thanks Jim
your reply, help me to understand the return of this function.

I read this before, and it confuses me !
Return Value

A value different from zero (i.e., true) if indeed c is a decimal digit. Zero (i.e., false) otherwise.
and since, there is no Boolean type in c
I thought, true equal "number different from zero"
false equal zero

English is my second language

best,

4. Originally Posted by JavaLover
and since, there is no Boolean type in c
I thought, true equal "number different from zero"
false equal zero
Yes, that is correct. In particular, the default "true" value is 1 (but generally you should not rely on this particular value, except where it is guaranteed).

5. just to clarify

Code:
```char c1='3';
char c2='k';
int f1,f2;

f1=isdigit(c1);
printf("\n%d",f1);
// output is 1
f2=isdigit(c2);
printf("\n%d\n",f2);
// output is 0```
so, now if I get 1 means the Var is a digit, zero if it is not

6. Originally Posted by laserlight
Yes, that is correct. In particular, the default "true" value is 1 (but generally you should not rely on this particular value, except where it is guaranteed).

Thanks Laserlight for the advice, I'll keep that in mind

Popular pages Recent additions