C语言 使用 isdigit() 和 isalpha() 命令的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2814194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:24:54  来源:igfitidea点击:

What is the simplest way of using isdigit() and isalpha() commands?

carraysscanfstdio

提问by NLed

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?

我需要怎样的两个命令的简要说明isdigit()isalpha()工作。当然,我在提出问题之前阅读了在线资源,但是我尝试了它们并且无法使它们起作用。使用它们的最简单方法是什么?

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?

假设,我想扫描一个包含数字和字母的文本文件并确定我要扫描的内容。这两个命令可以在这种情况下使用吗?

采纳答案by danben

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

它们不是“命令”,而是函数。函数接受参数并返回值。

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

This is the signature for the isdigitfunction: it indicates that it will accept an intvalue (or something that can be cast to int, like a char), and will return an int. You cannot, therefore, pass it an array (though you can call it on every member of an int[]).

这是isdigit函数的签名:它表示它将接受一个int值(或可以强制转换为 的东西int,如 a char),并将返回一个int。因此,您不能将数组传递给它(尽管您可以在 an 的每个成员上调用它int[])。

The signature for isalphais identical (except for the name, obviously).

的签名isalpha是相同的(显然,名称除外)。

The documentation says the following:

文档说明如下:

Description: The function isalpha() returns non-zero if its argument is a letter of the alphabet. Otherwise, zero is returned.

说明:如果函数 isalpha() 的参数是字母表中的一个字母,则该函数返回非零值。否则,返回零。

This means 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 falsein a boolean expression, and all non-zero values evaluate to true. So this check will cover implementations of isdigitthat return -1, 5, whatever.

在 C 中,0 将false在布尔表达式中计算为,并且所有非零值计算为true。因此,此检查将涵盖isdigit返回 -1、5 等的实现。

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.

如果要将这些应用于文本文件中的值,则必须一次读取一个字符的文本并将接收到的字符传递给这些方法。

回答by Jerry Coffin

You do notnormally want to compare the return value, just treat it as a Boolean:

不是通常要比较的返回值,只是把它当作一个布尔值:

if (isalpha(someinput))
    do_whatever();

If you insist on doing a comparison, it needs to be !=0, but it's entirely redundant and pointless.

如果您坚持要进行比较,则必须是!=0,但这完全是多余且毫无意义的。

You use it on charactersthat have been read from input, which are not (at that point) floats or anything, just groups of characters. In the case of a float, some of those will be digits, but many will also include decimal points (which aren't digits) and possibly an occasional e, +or -as well.

您可以在从输入中读取的字符上使用它,这些字符(此时)不是浮点数或任何东西,只是字符组。在浮点数的情况下,其中一些将是数字,但许多还包括小数点(不是数字),可能偶尔还有e+或者-也包括。

Also note that you normally need to cast the input to unsigned charbefore calling any of the is*functions. Many character sets will treat some characters as negative when they're viewed as signed char, and passing a negative value to any of these gives undefined behavior.

另请注意,您通常需要unsigned char在调用任何is*函数之前将输入强制转换为。许多字符集在将某些字符视为 时会将其视为负数signed char,并且将负值传递给其中任何一个会产生未定义的行为。

回答by LukeN

You can use it for any chartype! (actually it's int, I don't know whether this is for compatibilty reasons or whatnot, but the most common usage is for characters)

您可以将它用于任何字符类型!(实际上它是 int,我不知道这是出于兼容性原因还是其他原因,但最常见的用法是用于字符)

It will determine whether for example '5' is a digit or an alphanumerical (part of the alphabet). And yes, your usage is correct if someInputis of type "char"

例如,它将确定“5”是数字还是字母数字(字母表的一部分)。是的,如果someInput是“char”类型,则您的用法是正确的