const int still not enough for case statement?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Harry2o
    New Member
    • Oct 2009
    • 6

    const int still not enough for case statement?

    I stumbled upon the gcc error "case label does not reduce to an integer constant" when trying to use a const int variable in a case statement.

    Basically what is discussed in this thread: http://bytes.com/topic/c/answers/617...ant-expression

    Nevertheless all the provided solutions don't work for me (except using #define apparently).

    If I just have a simple program like:
    Code:
    int main( int argc, char** argv ) {
     int switcher = 0;
     const int testconst = 1337;
    
     switch( switcher ) {
      case testconst:
       break;
     }
    }
    it (3.3.5 as well as 4.3.3) still gives me that error.

    What am I doing wrong?
  • Harry2o
    New Member
    • Oct 2009
    • 6

    #2
    Hmm ... I guess it's because GCC doesn't allow that whereas g++ does (and why it worked in the other thread).

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Is this C or C++ code?

      it makes a difference, in C the const qualifier is really just a hint to the compiler that the variable can not be changed the symbol still represents a variable which can not be used where a constant value is required. You can not use such a variable as the size specifier when declaring an array either.

      That is why in many C programs you tend to find actual constant values #defined rather than declared as const variables.

      In C++ the const keyword has a much stronger meaning. It creates an actual constant value that on the whole can be used in switch cases and array declarations as long the variable has internal linkage (as opposed to external linkage).

      A const with external linkage can not be used because its value has to be resolved by referring to the memory location in which it is stored, which can not be done at compile time, so it doesn't actually reduce to a constant value at compile time.

      Comment

      • Harry2o
        New Member
        • Oct 2009
        • 6

        #4
        It is C code.

        And I understand what you're saying, but I would rather not use a switch statement than having to use #defines only for that reason. (#defines prevent various compiler warnings/errors, such as type checking and so forth)

        Would you see using enums (even if it's just one constant you want to use) as a reasonable workaround or solution?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You could use enums but they are little better than #defines except that they can only represent integer values.

          While #defines are not type safe since C is less type safe than say C++ anyway I do not think you will find much advantage over using #defines rather than const int or enums.

          However if you wish to give a more specific example of the sort of error you think wont be caught by use of #define (or a link) I will consider it.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Originally posted by Harry2o
            It is C code.
            And I understand what you're saying, but I would rather not use a switch statement than having to use #defines only for that reason. (#defines prevent various compiler warnings/errors, such as type checking and so forth)
            That might be true for C++, but are you sure there is a limitation like this for C? I can't think of any C compiler type checking warnings/errors that are fooled by #defines that expand to literal numbers.

            Are you familliar with the C literal number suffix notation?
            For instance: 24uL is an unsigned long constant with the value "24".

            Comment

            Working...