cctypeisalpha

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


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.


Solution

  • 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.