What is the meaning of return value of isalpha (at other ctype.h functions)?

636 Views Asked by At

The return value of isalpha() is 0 if the character is not alphabet and non-zero if its an alphabet. This is also the case for many other ctype.h library functions.

Is there any meaning of the return type of this function?
In other words, why not simply return 1 for alphabet characters?
I googled and did not find any answer.

2

There are 2 best solutions below

2
On BEST ANSWER

The return value is not specified, because it might save a few cycles for common implementations to not have to set it to 1, if it is not zero. For example, the inequality-test could be implemented as subtraction

#define NEQ(a,b) (a-b)

If you require to have it return 1 on inequality, you have to do a little more work than only the subtraction. Hence, it might perform better to not insist that the functions return 1.

1
On

It returns an integer value that is nonzero if the character is a letter and zero otherwise. It returns an int because that is the default return value from ancient C, and it would require unnecessary code (with little benefit) to restrict the values returned to 0 and 1, considering that these functions are meant to be used in conditions.

You may have also noticed that all of these functions have a name shorter than or as long as eight characters. This is, again, a relic from ancient C. Names of functions could be no longer than 8 characters.