Other answers have explained the requirements of C syntax, specifically that each case
value needs to be a constant expression. I just want to add that the example code you presented is semantically flawed, too.
You wrote:
switch(input) {
case isalnum(input):
...
A switch
statement compares the switch expression -- input
, in this case -- with each of the case
expressions, in search of a match. But isalnum()
returns an unspecified non-zero number when its argument is alphanumeric, and zero otherwise. There is no reason to expect that input == isalnum(input)
will be true when input
is in fact alphanumeric (though it nevertheless might be), whereas you can be certain that that relationship does hold for the argument 0
.
Thus, this specific approach would be incorrect even if C permitted case
expressions to be non-constant.