473,395 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

isdigit

mdh
Could I get some help as to understanding why I am not getting a result
I expect.
#include <stdio.h>
#include <ctype.h>

int main (){
int i,j,k,c;
i=9;
c='A';
j=isdigit(9);
k=isdigit('A');
printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);

return 0;
}

Output:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 0

Expected:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 1

Oct 6 '06 #1
10 5853
mdh wrote:
Could I get some help as to understanding why I am not getting a result
I expect.
#include <stdio.h>
#include <ctype.h>

int main (){
int i,j,k,c;
i=9;
c='A';
j=isdigit(9);
k=isdigit('A');
printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);

return 0;
}

Output:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 0

Expected:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 1
'A' is not a digit
9 is not a digit

'9' is a digit
Oct 6 '06 #2


On Oct 6, 2:02 pm, "mdh" <m...@comcast.netwrote:
Could I get some help as to understanding why I am not getting a result
I expect.

#include <stdio.h>
#include <ctype.h>

int main (){
int i,j,k,c;
i=9;
c='A';

j=isdigit(9);
k=isdigit('A');

printf("\nThe value of \"isdigit(%d)\" is %d", i,j);
printf("\nAnd the value of \"isdigit('%c')\" is %d\n", c,k);

return 0;

}Output:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 0

Expected:

The value of "isdigit(9)" is 0
And the value of "isdigit('A')" is 1
Do you consider 'A' as a hexadecimal digit? If so, use function
isxdigit() instead of isdigit().

Oct 6 '06 #3
mdh
'A' is not a digit
9 is not a digit

'9' is a digit

it's getting late...thank you

Oct 6 '06 #4
Answer; Since c is a integer variable,when we try to store the
charecter in the integer variable Ascii value of that charecter is
stored in the variable,since Ascii value of the charecter is also digit
hence the isdigit()
function returns true

Oct 6 '06 #5
j=isdigit(9);
k=isdigit('A');
Hi,
The isdigit() function takes an argument of character type or EOF and
it return non zero value if
the passing argument is characters '0' , '1' . . . '9' else it returns
zero.
Now consider your actual out put.

1. The first part of the out put is true as you have supplied wrong
argument i.e. a integer
insite of supplying char.
2. Second part is also true. No need to explain.

Regards,
Shafi

"the difficult we do immediately, the impossible takes a little
longer", [US Army

Oct 14 '06 #6
"shafi" <sh*********@gmail.comwrites:
>j=isdigit(9);
k=isdigit('A');

The isdigit() function takes an argument of character type or EOF and
it return non zero value if
the passing argument is characters '0' , '1' . . . '9' else it returns
zero.
More precisely,

... the argument is an int, the value of which shall be
representable as an unsigned char or shall equal the value of the
macro EOF. If the argument has any other value, the behavior is
undefined.

This is an important distinction. Something like this:

char c = some_value;
isdigit(c);

can invoke undefined behavior if (1) plain char is signed, and (2) the
value of c is negative (and not equal to EOF). For arguments other
than EOF, it's often a good idea to cast the argument:

char c = some_value;
isdigit((unsigned char)c);

(This is one of the few cases where a cast is actually a good idea.)

Or, in this case, you could avoid the cast by changing the declaration
of c:

unsigned char c = some_value;
isdigit(c);

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 14 '06 #7
On Fri, 2006-10-13 at 22:22 -0700, shafi wrote:
j=isdigit(9);
k=isdigit('A');

Hi,
The isdigit() function takes an argument of character type or EOF and
it return non zero value if
the passing argument is characters '0' , '1' . . . '9' else it returns
zero.
isdigit(c) function takes an argument of type int that must be in the
range of unsigned char or EOF. It returns nonzero on '0'...'9',
otherwise returns 0.
Now consider your actual out put.

1. The first part of the out put is true as you have supplied wrong
argument i.e. a integer
insite of supplying char.
Wrong. 9 is in the range of unsigned char, and therefore is perfectly
valid. Whether the expression evaluates to true or not depends on the
charset.
2. Second part is also true. No need to explain.
Wrong. 'A' is not a digit.

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>

Oct 14 '06 #8
Andrew Poelstra wrote:
>
On Fri, 2006-10-13 at 22:22 -0700, shafi wrote:
j=isdigit(9);
k=isdigit('A');
Hi,
The isdigit() function takes an argument
of character type or EOF and
it return non zero value if
the passing argument is characters
'0' , '1' . . . '9' else it returns zero.

isdigit(c) function takes an argument of type int that must be in the
range of unsigned char or EOF. It returns nonzero on '0'...'9',
otherwise returns 0.
Now consider your actual out put.

1. The first part of the out put is true as you have supplied wrong
argument i.e. a integer
insite of supplying char.

Wrong. 9 is in the range of unsigned char, and therefore is perfectly
valid. Whether the expression evaluates to true or not depends on the
charset.
It doesn't depend on the char set.
isdigit is one of the ctype functions
that does not have locale specific behavior.

--
pete
Oct 14 '06 #9
pete <pf*****@mindspring.comwrites:
Andrew Poelstra wrote:
[...]
>Wrong. 9 is in the range of unsigned char, and therefore is perfectly
valid. Whether the expression evaluates to true or not depends on the
charset.

It doesn't depend on the char set.
isdigit is one of the ctype functions
that does not have locale specific behavior.
The value of isdigit(9) depends on the character encoding, which can
vary from one implementation implementation to another. (I don't know
whether "char set" is the right term for this.)

I believe it returns 0 on all existing implementations, but 9 could
theoretically be the encoding for some digit (but it can't be the
encoding for '9', since the digits are contiguous and '\0' can't be a
digit).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 14 '06 #10
Keith Thompson wrote:
>
pete <pf*****@mindspring.comwrites:
Andrew Poelstra wrote:
[...]
Wrong. 9 is in the range of unsigned char, and therefore is perfectly
valid. Whether the expression evaluates to true or not depends on the
charset.
It doesn't depend on the char set.
isdigit is one of the ctype functions
that does not have locale specific behavior.

The value of isdigit(9) depends on the character encoding, which can
vary from one implementation implementation to another. (I don't know
whether "char set" is the right term for this.)

I believe it returns 0 on all existing implementations, but 9 could
theoretically be the encoding for some digit (but it can't be the
encoding for '9', since the digits are contiguous and '\0' can't be a
digit).
I've been very sloppy in my postings lately.
I saw 9 but read it as '9'.

--
pete
Oct 14 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: James Gregory | last post by:
I read that the argument to isdigit() can be "an integer whose value is representable as an unsigned char, or the value of the macro EOF.". This seems to say that it should work for values greater...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
7
by: Per Rollvang | last post by:
Hi All! I have a problem with hex strings when using Char.IsDigit(). The hex strings(i.e. a CRC32 checksum) get a false result for letters A-F... MSDN tell me that: Indicates whether a...
2
by: Jefe | last post by:
Hi Group Could you please explain to me what's the difference between Chars.IsDigit and Chars.IsNumber? Regards,
16
by: skneife | last post by:
Doeas any know the simplest way for create this type of function: bool result = IsDigit(string s); Sam
3
by: MooMaster | last post by:
N00b question alert! I did a search for isdigit() in the group discussion, and it didn't look like the question had been asked in the first 2 pages, so sorry if it was... The manual...
2
by: bozo789 | last post by:
Hi Forum, First let me begin by stating I am a student and have read the proper posting procedure concerning students. I am a begginer learning with the Miracle C Work Bench. I know my code is...
1
by: khaichiew85 | last post by:
im having a program.I want it to check if it is a character or integer is entered where shall i put the isDigit and how to use?i put it as highlighted below but got error. #include<stdio.h>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.