Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

c - isalpha() giving an assertion

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:

enter image description here

char c='£';

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

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

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

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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


Edited to add an explanation.

According to the standard, emphasis is mine

7.4

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...