使用 isdigit() 和 isalpha() 命令的最简单方法是什么?
我需要简要解释一下 isdigit()
和 isalpha()
这两个命令的工作原理。当然,我在提出问题之前阅读了在线资源,但我尝试过它们但无法让它们工作。使用它们的最简单方法是什么?
我知道它返回一个值,所以我假设我可以像这样使用它:
if(isdigit(someinput)==1)
return -1;
这是正确的吗?我可以将其用于任何类型的角色吗?我可以将它与浮点数或数组进行比较吗?
假设,我想扫描一个包含数字和字母的文本文件,并确定我正在扫描的内容。这两个命令可以在这种情况下使用吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您通常不想比较返回值,只需将其视为布尔值即可:
如果您坚持进行比较,则它需要为
!=0
,但它是完全多余且毫无意义。您可以在从输入读取的字符上使用它,这些字符(此时)不是浮点数或任何东西,只是字符组。对于浮点数,其中一些将是数字,但许多也将包含小数点(不是数字),并且可能偶尔包含
e
、+
或-
也是如此。另请注意,在调用任何
is*
函数之前,您通常需要将输入转换为unsigned char
。当某些字符被视为有符号字符时,许多字符集会将它们视为负数,并且将负值传递给其中任何一个都会导致未定义的行为。You do not normally want to compare the return value, just treat it as a Boolean:
If you insist on doing a comparison, it needs to be
!=0
, but it's entirely redundant and pointless.You use it on characters that 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.Also note that you normally need to cast the input to
unsigned char
before calling any of theis*
functions. Many character sets will treat some characters as negative when they're viewed assigned char
, and passing a negative value to any of these gives undefined behavior.它们不是“命令”,而是函数。函数接受参数并返回值。
这是
isdigit
函数的签名:它表明它将接受int
值(或者可以转换为int
的值,例如achar
),并将返回一个int
。因此,您不能向其传递一个数组(尽管您可以对int[]
的每个成员调用它)。isalpha
的签名是相同的(显然,名称除外)。该文档说明如下:
这意味着您的比较并不适用于所有实现。最好这样做:
在 C 中,布尔表达式中的 0 将计算为
false
,所有非零值将计算为true
。因此,此检查将涵盖返回 -1、5 等的isdigit
实现。如果要将这些应用于文本文件中的值,则必须一次读取一个字符,并将收到的字符传递给这些方法。
They are not "commands", they are functions. Functions accept arguments and return values.
This is the signature for the
isdigit
function: it indicates that it will accept anint
value (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
isalpha
is 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
false
in a boolean expression, and all non-zero values evaluate totrue
. So this check will cover implementations ofisdigit
that 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.
您可以将它用于任何char类型! (实际上它是 int,我不知道这是否是出于兼容性原因或其他原因,但最常见的用法是字符)
它将确定例如“5”是数字还是字母数字(字母表的一部分)。是的,如果
someInput
的类型为“char”,那么您的用法是正确的You can use it for any char type! (actually it's int, I don't know whether this is for compatibilty reasons or whatnot, but the most common usage is for characters)
It will determine whether for example '5' is a digit or an alphanumerical (part of the alphabet). And yes, your usage is correct if
someInput
is of type "char"