What is the simplest way to use the isdigit () and isalpha () commands?

I need a short description of how the two commands work isdigit()

and isalpha()

. Of course I read online sources before asking the question, but I tried them and couldn't get them to work. What's the easiest way to use them?

I know this returns a value, so I guess I can use it like this:

if(isdigit(someinput)==1)
 return -1;

      

It is right? Can I use this for any type of character? Can I compare it to a number or floating point array?

Let's say I want to scan a text file with numbers and a letter and determine what I'm scanning. Can these two commands be used in this context?

+2


a source to share


3 answers


They are not "commands", they are functions. Functions take arguments and return values.

#include <ctype.h>
int isdigit( int ch );

      

This is the signature for the function isdigit

: it indicates that it will accept a value int

(or something that can be appended to int

, for example char

) and will return a int

> symbol . Therefore, you cannot pass an array (although you can call it for each member int[]

).

The signature for is isalpha

identical (except for the name, obviously).

The documentation states the following:



Description: The isalpha () function returns nonzero if its argument is an alphabet letter. Otherwise, zero is returned.

This means that your comparison will not be correct for all implementations. Better to do something like:

if (isdigit(someinput)) {
 return -1;
}

      

In C, 0 will evaluate to false

in a boolean expression, and all non-zero values ​​will evaluate to true

. So this check will cover implementations isdigit

that return -1, 5, whatever.

If you want to apply them to values ​​in a text file, you must read the text one character at a time and pass the characters you receive to these methods.

+1


a source


Usually you don't want to compare the return value, just treat it as boolean:

if (isalpha(someinput))
    do_whatever();

      

If you insist on making a comparison, it should be !=0

, but it is completely redundant and pointless.



You use it on characters that have been read from input that are not (at this point) floats or whatever, only groups of characters. In the case of floating point, some of them will be digits, but many will also contain decimal points (which are not digits) and possibly random ones e

, +

or -

.

Also note that you usually need to type input into unsigned char

before calling any of the functions is*

. Many character sets will treat some characters as negative when viewed as signed char

, and passing a negative value to any of them gives undefined behavior.

+2


a source


You can use it for any char type ! (it is actually an int, I don't know if this is for compatibility reasons or something else, but the most common usage is for symbols)

It will determine if, for example, "5" is a number or alphanumeric (part of the alphabet). And yes, your usage is correct if it someInput

is of type "char"

0


a source







All Articles