Reputation: 95
I am using the following code. I expect the output to be "Yes", but I instead get "No." I must be missing something very simple and fundamental.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int n = 3;
if (isdigit(n))
{
printf("Yes\n");
}
else
{
printf("No\n");
}
return 0;
}
Upvotes: 1
Views: 150
Reputation: 30136
Here is the implementation of function isdigit
:
int isdigit(char c)
{
return '0' <= c && c <= '9';
}
In simple words, function isdigit
takes a char
argument as input:
If the input represents a decimal digit in ASCII format, then the function returns 1.
If the input does not represent a decimal digit in ASCII format, then the function returns 0.
When you call function isdigit
with an int
argument, the argument is first truncated to a char
. That doesn't make any difference in your example, since 3 fits into a char
, so no information is lost due to the truncation. However, since 3 does not represent any decimal digit in ASCII format, the return-value of isdigit(3)
is 0.
To summarize the above:
isdigit('0')
is 1.isdigit('1')
is 1.isdigit('9')
is 1.isdigit(...)
is 0.Your mistake probably stems from the assumption that '3' == 3
. If you want to check whether or not an int
variable stores a single-decimal-digit value, then you can implement a slightly different function:
int isintdigit(int i)
{
return 0 <= i && i <= 9;
}
Upvotes: 0
Reputation: 108978
The string "\03\t\nABCabc123"
is composed of 12 characters. The first 9 characters all return false when applied to the isdigit()
function. The last 3 all return true;
#include <ctype.h>
#include <stdio.h>
int main(void) {
char data[] = "\03\t\nABCabc123";
// data[0] = 3;
char *ptr = data;
while (*ptr) {
printf("%4d --> %s\n", *ptr, isdigit((unsigned char)*ptr) ? "true" : "false");
ptr++;
}
return 0;
}
Upvotes: 0
Reputation: 18831
man isdigit
:
These functions check whether c, which must have the value of an
unsigned char or EOF, falls into a certain character class according to
the current locale.
3 is an integer, not a character. In fact, 3 is also the value of a character that is not a digit.
Upvotes: 0
Reputation: 106012
isdigit
expects a character. If the character passed is digit then it returns non zero. 3
is an ASCII value of non-printable character, i.e it doesn't represent a digit and that's why your else
body gets executed.
Upvotes: 0
Reputation: 9680
isdigit
checks whether the character passed to it is a numeric character. Therefore, its argument should be char
type or int
type which is the code of a character.
Here, you are passing 3
to isdigit
. In ASCII, 3
is the code of the character ETX
(end of text) which is a non-numeric character. Therefore, isdigit(3)
returns false
.
Upvotes: 2
Reputation: 25908
'3' is not a digit, here, in the way isdigit()
considers them. Change int n = 3
to int n = '3'
. If your system uses ASCII, for instance, 3
is the end of text marker, whereas 51
, equivalent to '3'
, is the actual character three.
Upvotes: 1
Reputation: 45654
isdigit()
expects a character code, while you expect it to accept a plain number.
Those expectations do not mesh well.
Character literals:
'0'
ordinal 0x30 -- 48'1'
ordinal 0x31 -- 49'2'
ordinal 0x32 -- 50Upvotes: 2