Hello,

#include <stdio.h>

int main(void)
{
        const int j = 2;
        int i;
 
        switch (i) {
                case 1:break;
                case j:break;
                default:break;
        }
 
        return 0;

}

i tried executing the above code, and i got the following error:
error: case label does not reduce to an integer constant

Well, since i have declared j as const , should it not take it as a constant?
Can anyone please explain?

Thanks.

Recommended Answers

All 8 Replies

Try this

#include <stdio.h>

#define j 2

int main(void)
{

        int i;
 
        switch (i) {
                case 1:break;
                case j:break;
                default:break;
        }
 
        return 0;

}

or

#include <stdio.h>

enum choices {a, b, c};

int main(void)
{

	int i = 0;

	fputs("enter a value->", stdout);
	fscanf(stdin, "%d", &i);

	switch (i) 
	{
	case a:
	{
		fputs("a\n", stdout);
		break;
	}
	case b:
	{
		fputs("b\n", stdout);
		break;
	}
	default:break;
	}

	return 0;

}

Thanks for the reply.

Yes. This works fine. But im wondering why the former did not work inspite of the const qualifier.

Thanks for the reply.

Yes. This works fine. But im wondering why the former did not work inspite of the const qualifier.

Try running this code with a 'const int'

#include <stdio.h>

int main(void)
{
	int *iptr = (int*)NULL;
	const int i = 1234;

	fprintf(stdout, "i->%d\n", i);

	iptr = (int*)&i;

	*iptr = 898989;

	fprintf(stdout, "i->%d\n", i);

	return 0;

}

Because in C, const != "constant", but "read-only". That is, it is legal to initialize a const variable using

void f(int n) {
    
    const int m = n / 2;
}

but its value is dependent of n. So if we call f passing first 2 as argument, and then 4, the values of m will differ. But a switch statement requires integer constants which are known before the program executes. In the above example, this is clearly not the case.

@creeps
Thanks a lot! i got it. i had misunderstood the const qualified to mean "constant".
Thanks for the clarification.

@gerard4143
Thanks.

Because in C, const != "constant", but "read-only". That is, it is legal to initialize a const variable using

void f(int n) {
    
    const int m = n / 2;
}

but its value is dependent of n. So if we call f passing first 2 as argument, and then 4, the values of m will differ. But a switch statement requires integer constants which are known before the program executes. In the above example, this is clearly not the case.

I don't like the term "read-only" because in this case it was read/write if you knew enough to step around the compiler protection.

> step around the compiler protection...
... straight into the undefined behaviour.

> step around the compiler protection...
... straight into the undefined behaviour.

Yes true enough..But I think we moved away from the original problem of why

#define j 2

or

enum choice {a, b, c};

works but

const int j = 2;

does not...IMHO I really feel its because the enums and the define are literals which are written into the instructions while const int j is a variable which has to be fetched from memory...

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.