I need a brief explanation on how the two commands isdigit() and isalpha() work. Of course, I read online sources before asking the question, but I tried them and couldn’t get them to work. What is the simplest way of using them?
I know it gives back a value, so I’m assuming I can use it like this:
if(isdigit(someinput)==1)
return -1;
Is that correct? Can I use this for any type of character? And can I compare it with a float number or array?
Suppose, I want to scanf a text file that has numbers and letter and determine what I’m scanning. Can these two commands be used in this context?
They are not “commands”, they are functions. Functions accept arguments and return values.
This is the signature for the
isdigitfunction: it indicates that it will accept anintvalue (or something that can be cast toint, like achar), and will return anint. You cannot, therefore, pass it an array (though you can call it on every member of anint[]).The signature for
isalphais identical (except for the name, obviously).The documentation says the following:
This means your comparison will not be correct for all implementations. Better to do something like:
In C, 0 will evaluate to
falsein a boolean expression, and all non-zero values evaluate totrue. So this check will cover implementations ofisdigitthat return -1, 5, whatever.If you want to apply these to values in a text file, you must read the text one character at a time and pass the characters you receive to those methods.