因而isalpha()给予断言 [英] isalpha() giving an assertion

查看:220
本文介绍了因而isalpha()给予断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我使用标准库函数因而isalpha()在ctype.h中一个C code,这是Visual Studio 2010的Windows的。
在下面code,如果字符c是£时,因而isalpha调用返回的断言,如下面的快照:

I have a C code in which I am using standard library function isalpha() in ctype.h, This is on Visual Studio 2010-Windows. In below code, if char c is '£', the isalpha call returns an assertion as shown in the snapshot below:

char c='£';

if(isalpha(c))
{
    printf ("character %c is alphabetic\n",c);

}
else
{
    printf ("character %c is NOT alphabetic\n",c);
}

我可以看到,这可能是因为8位ASCII不具有这种性格。

I can see that this might be because 8 bit ASCII does not have this character.

那么,如何处理ASCII表外等非ASCII字符?

So how do I handle such Non-ASCII characters outside of ASCII table?

如果任何非字母字符被找到(即使它包括这样的字符不是8位的ASCII表),我希望能够忽视它我想要做的就是

What I want to do is if any non-alphabetic character is found(even if it includes such character not in 8-bit ASCII table) i want to be able to neglect it.

推荐答案

您可能要投送到因而isalpha 值(和<$ C申报的其他功能$ C>&LT;&文件ctype.h GT; )以 unsigned char型

You may want to cast the value sent to isalpha (and the other functions declared in <ctype.h>) to unsigned char

isalpha((unsigned char)value)

这是(不那么)几个场合铸造是在C合适的一个。

It's one of the (not so) few occasions where a cast is appropriate in C.

编辑补充解释。

根据标准,重视是我的

7.4

1头&LT;&文件ctype.h GT; 声明有用的多种功能进行分类和映射
  字符。在所有情况下的参数为 INT ,其值应为
  再presentable为 unsigned char型或应等于宏观 EOF 的价值。 如果在
  说法有任何其他的价值,行为是不确定的。

1 The header <ctype.h> declares several functions useful for classifying and mapping characters. In all cases 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.

演员到 unsigned char型确保调用因而isalpha()不调用未定义行为。

The cast to unsigned char ensures calling isalpha() does not invoke Undefined Behaviour.

这篇关于因而isalpha()给予断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆