1

I want to read characters from the console and check if it is alpha numeric. From my understanding, isalnum has the following function signature.

int isalnum(int x);

Why is it illegal to write

switch(input) { case isalnum(input): ...

if isalnum returns an integer value.

Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
  • 2
    `case` values must be constants or literals. – Nick Jul 31 '18 at 19:48
  • 1
    try switch(isalnum(input)) - case 1: - case: 0 – emsimpson92 Jul 31 '18 at 19:49
  • 1
    Why not `if(isalnum(input)) { ... }`? – BeyelerStudios Jul 31 '18 at 19:49
  • @BeyelerStudios The purpose of the exercise i'm working on is to use switch statements. – Mutating Algorithm Jul 31 '18 at 19:50
  • 1
    @emsimpson92, you mean `case 0:` and `default:` – David Ranieri Jul 31 '18 at 19:52
  • ah... I'm used to C# where default: isn't required. – emsimpson92 Jul 31 '18 at 19:53
  • 4
    @emsimpson92, in c `default` is also optional, but I mean `isalnum` return value is _A value **different from zero** (i.e., true) if indeed c is either a digit or a letter. Zero (i.e., false) otherwise._ – David Ranieri Jul 31 '18 at 19:55
  • To the OP, there's probably some benefit in learning exactly how a `switch` operates in C. Even if this were possible, it probably would not do what you want anyway. At a wild guess you're trying to do what Scuzzy's answer to [this question](https://stackoverflow.com/questions/8829229/why-switchtrue-in-php-with-a-strange-logic) shows. That's possible in PHP, but it is simply not possible in C. – dgnuff Jul 31 '18 at 20:03

5 Answers5

4

See https://en.cppreference.com/w/c/language/switch

The syntax requires

case constant_expression : statement

with

constant_expression - a constant expression of the same type as the type of condition after conversions and integral promotions

I.e. syntax requires constant expression.
The result of a function call is not a constant expression.
That is why it is illegal.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
2

You can't call isalnum in your case statement. Try something like this:

switch(isalnum(input))
{
  case 0:
    //do stuff
    break;
  default:
    //do other stuff
    break;
}
emsimpson92
  • 1,779
  • 1
  • 9
  • 24
2

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

switch statement transfers the control flow of the program to one of the cases listed. That's why you need to assigne constant values to case. They are evaluated at compiled time.

jplc
  • 354
  • 1
  • 11
0

If the purpose is to use the switch-case statement, then maybe ditch isalnum and just have a bunch of cases for each alphanumeric character with fall-through and then a default case. Something like:

switch(input)
{
    case '0':
    case '1':
    case '2':

[...]

    case 'z':
        // do something here
        break;
    default:
        // do something else here
        break;
}
Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29