isalpha() giving an assertion

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.

Leave a Comment