Using const int as a switch case

I have a strange dilemma:

const int querry_pos = 2, id_pos=0, who_pos = 1, date_pos = 3, reservist_id_pos = 4; 

            switch (j) {
                case id_pos:{           id           = res.get_value (i, j); break;}
                case who_pos:{          who          = res.get_value (i, j); break;}
                case querry_pos:{       querry       = res.get_value (i, j); break;}
                case date_pos:{         date         = res.get_value (i, j); break;}
                case reservist_id_pos:{ reservist_id = res.get_value (i, j); break;}

With this code i have error:

 ./V. vala:105:9: error: case-label value is irreducible to integer constant
  105 | switch (j) {
          | ^

And without const:

v.vala:106.22-106.27: error: Expression must be constant
                case id_pos:{           id           = res.get_value (i, j); break;}

Is it possible to use constants as a case statement?

For most programming languages the case labels are integer constants, but I don’t know vala.

From error messages, may data type of your variable j be wrong, maybe not compatible with integer data tape?

But it should be easy to find out for yourself – look for a working switch code example from a vala tutorial and compare to your code :slight_smile:

I hid the context to simplify the example, j – iterator nested loop, type int of course.


I may get the feeling that you have to use something like

const int querry_pos = 2
const  id_pos = 0

So a const keyword for every const label.

Found Vala switch example only with int literals, so maybe test above.

Maybe tomorrow someone who have used Vala already can answer.


If Vala works like C (and by necessity it has to) then you can only use constant expressions as the C standard mandates, that is: expressions that evaluate to constants at compile time:

A constant expression can be evaluated during translation rather than runtime, andaccordingly may be used in anyplace that a constant may be. (ISO/IEC 9899:TC3, 6.6)

Additionally, as the error message you get clarifies, you will need an integer constant expression:

An integer constant expression shall have integer type and shall only have operandsthat are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof operator. (ibid.)

A const variable can be reassigned, which means it cannot be a constant expression; sizeof (Foo), on the other hand, is a constant expression because the size of Foo is known at compile time and cannot change at run time.

Your use-case wants an enumeration, anyway.

1 Like

I’m not sure what you program is doing (especially the j), but trying to reproduce what you said I get the following (working) example.

const int querry_pos = 2;
const int id_pos=0;
const int who_pos = 1;
const int date_pos = 3;
const int reservist_id_pos = 4; 

int main() {
    for (int j = 0; j < 10; j++) {
        switch (j) {
        case id_pos:
        case who_pos:
        case querry_pos:
        case date_pos:
        case reservist_id_pos:
    return 0;


In C the const qualifier is just a “promise” to the Compiler that you are not going to mess up with the variable.

In other words there is no constant integer here:

const int variable = value;

The only true constants in C are those like:

#define VARIABLE value

And integer constants like:

char ch = ‘a’;

Now ‘a’ Is constant.

Everything fell into place.
If you declare variables locally then in c there generating
static const gint querry_pos = 2;
And its define if outside,
#define querry_pos 2
Thats intresting.

(Swich works only with define, ye)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.