0
#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isdigit('1'):printf("Hello");
    }
}

This code works fine Hello is printed

#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isalpha('a'):printf("Hello");
    }
}

But this code gives an error
It says case label does not reduce to an integer

7
Contributors
8
Replies
10
Views
5 Years
Discussion Span
Last Post by WaltP
0

It says case label does not reduce to an integer

Maybe printing isalpha('a') might help. Just because isalpha on true returns non-zero integer , not necessarily 1. So can be a problem. Just try that out

0

Neither case will work for me. The tags for case statements must be constants, they can not be the result of a function call.

It might be the case that, on your platform, the isdigit is evaluated at pre-processor time to be 1 allowing for you to compile. Can you show the result of the pre-processor on your first example?

0

What exactly are you trying to do?

isdigit('1') will always have the same value. isalpha('a') will always have the same value. Why bother using a function to generate values that never change and are always the same?

What are you trying to do here?

0

@DSJAN10
yea u r right isalpha('a') returned 1024 when i printed it.

@L7sqr
Well they can be the result of an expression right?? case : 'B'>'A' is right. As long as it evaluates to an integer or character constant (with no variables in the expression) it works fine. Anyway wat did u pre processor time?
@Moschops
Absolutely nothing..one of my students had this doubt...none of the functions work with case except isdigit.

0
#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isdigit('1'):printf("Hello");
    }
}

This code works fine Hello is printed

#include<stdio.h>
main()
{
    int a=1;
    switch(a)
    {
        case isalpha('a'):printf("Hello");
    }
}

But this code gives an error
It says case label does not reduce to an integer

It doesn't reduce to a *constant integer expression*. So no, that code shouldn't compile because it has a constraint violation.

Edited by deceptikon: n/a

0

@L7sqr
Well they can be the result of an expression right?? case : 'B'>'A' is right. As long as it evaluates to an integer or character constant (with no variables in the expression)

A function call (with a variable input) is not a constant expression. It might be the case that a compiler is tuned to evaluate a lookup-table macro expansion and replace it with a constant expression given a constant input but I wouldn't count on such behavior.

Edited by L7Sqr: n/a

0

Case expression should be a constant or an expression that reduces to a constant.

It surprises me that no one before you said anything even remotely like that! Oh wait...! Look at what L7QSR and deceptikonsaid... :icon_rolleyes:

Edited by WaltP: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.