#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 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

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?

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?

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

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

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

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

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.