case label does not reduce to an integer constant

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rex Mottram

    case label does not reduce to an integer constant

    Can anyone explain this?

    % cat t.c
    #include <stdio.h>

    #define FIRST "first"
    #define SECOND "second"

    int
    main(int argc, char *argv[])
    {
    switch (argv[1][0]) {
    case FIRST[0]:
    case 'f':
    printf("%s == FIRST\n", argv[1]);
    break;
    case SECOND[0]:
    case 's':
    printf("%s == SECOND\n", argv[1]);
    break;
    }
    return 0;
    }

    % gcc -o t -W -Wall -g t.c
    t.c: In function `main':
    t.c:10: error: case label does not reduce to an integer constant
    t.c:14: error: case label does not reduce to an integer constant

    It certainly looks constant to me, and it should be promotable to an
    integer exactly as the neighboring case label is. This is gcc 3.4.5 FWIW.

    TIA,
    RM
  • Malcolm McLean

    #2
    Re: case label does not reduce to an integer constant


    "Rex Mottram" <rexm@not.herew rote in message
    news:5JmdnaLkgp QwPJ3VnZ2dnUVZ_ uKpnZ2d@comcast .com...
    Can anyone explain this?
    >
    % cat t.c
    #include <stdio.h>
    >
    #define FIRST "first"
    #define SECOND "second"
    >
    int
    main(int argc, char *argv[])
    {
    switch (argv[1][0]) {
    case FIRST[0]:
    case 'f':
    printf("%s == FIRST\n", argv[1]);
    break;
    case SECOND[0]:
    case 's':
    printf("%s == SECOND\n", argv[1]);
    break;
    }
    return 0;
    }
    >
    % gcc -o t -W -Wall -g t.c
    t.c: In function `main':
    t.c:10: error: case label does not reduce to an integer constant
    t.c:14: error: case label does not reduce to an integer constant
    >
    It certainly looks constant to me, and it should be promotable to an
    integer exactly as the neighboring case label is. This is gcc 3.4.5 FWIW.
    >
    Yes, you've managed to fool it.

    However sections of string literals aren't legal in switch statements, only
    character constants. It reduces the work the parser has to do.

    --
    Free games and programming goodies.



    Comment

    • Pradeep

      #3
      Re: case label does not reduce to an integer constant

      On Apr 12, 5:49 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
      "Rex Mottram" <r...@not.herew rote in message
      >
      news:5JmdnaLkgp QwPJ3VnZ2dnUVZ_ uKpnZ2d@comcast .com...
      >
      Can anyone explain this?
      >
      % cat t.c
      #include <stdio.h>
      >
      #define FIRST "first"
      #define SECOND "second"
      >
      int
      main(int argc, char *argv[])
      {
      switch (argv[1][0]) {
      case FIRST[0]:
      case 'f':
      printf("%s == FIRST\n", argv[1]);
      break;
      case SECOND[0]:
      case 's':
      printf("%s == SECOND\n", argv[1]);
      break;
      }
      return 0;
      }
      >
      % gcc -o t -W -Wall -g t.c
      t.c: In function `main':
      t.c:10: error: case label does not reduce to an integer constant
      t.c:14: error: case label does not reduce to an integer constant
      >
      It certainly looks constant to me, and it should be promotable to an
      integer exactly as the neighboring case label is. This is gcc 3.4.5 FWIW.
      >
      Yes, you've managed to fool it.
      >
      However sections of string literals aren't legal in switch statements, only
      character constants. It reduces the work the parser has to do.
      >
      --
      Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm

      Hi,
      switch case in c language accepts only integer values.
      If you are passing character variable then you have put the same in
      single quotations (i.e.,'')
      That will indicate ascii value of the character.
      But in your program you have written as
      case FIRST[0]:
      here "FIRST[0]->f" this is character.so you need to put the same in
      single quotations.
      so replace your code from
      -------------------
      case FIRST[0]:
      -->to
      case 'FIRST[0]':
      -------------------
      similarly chanage
      -------------------
      case SECOND[0]:
      -->to
      case 'SECOND[0]':
      ---------------------

      --Pradeep

      Comment

      • Richard Heathfield

        #4
        Re: case label does not reduce to an integer constant

        Pradeep said:

        <snip>
        >
        switch case in c language accepts only integer values.
        Yes, case statements must be an integral constant expression.
        If you are passing character variable then you have put the same in
        single quotations (i.e.,'')
        You can't "pass" a character, or indeed anything, to a switch.
        That will indicate ascii value of the character.
        No, it won't.
        But in your program you have written as
        case FIRST[0]:
        here "FIRST[0]->f" this is character.so you need to put the same in
        single quotations.
        so replace your code from
        -------------------
        case FIRST[0]:
        -->to
        case 'FIRST[0]':
        That isn't even close. In fact, it's so far the wrong side of quite a long
        way out that even to see just how wrong it is you'd need extremely
        powerful erroculars.

        --
        Richard Heathfield <http://www.cpax.org.uk >
        Email: -http://www. +rjh@
        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
        "Usenet is a strange place" - dmr 29 July 1999

        Comment

        • Richard Heathfield

          #5
          Re: case label does not reduce to an integer constant

          Rex Mottram said:
          Can anyone explain this?
          >
          % cat t.c
          #include <stdio.h>
          >
          #define FIRST "first"
          #define SECOND "second"
          >
          int
          main(int argc, char *argv[])
          {
          switch (argv[1][0]) {
          case FIRST[0]:
          "An integral constant expression shall have integral type and shall
          only have operands that are integer constants, enumeration constants,
          character constants, sizeof expressions, and floating constants that
          are the immediate operands of casts."

          A string literal is none of those. The fact that you're indexing it is
          neither here nor there.

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Bartc

            #6
            Re: case label does not reduce to an integer constant


            "Rex Mottram" <rexm@not.herew rote in message
            news:5JmdnaLkgp QwPJ3VnZ2dnUVZ_ uKpnZ2d@comcast .com...
            Can anyone explain this?
            >
            % cat t.c
            #include <stdio.h>
            >
            #define FIRST "first"
            #define SECOND "second"
            >
            int
            main(int argc, char *argv[])
            {
            switch (argv[1][0]) {
            case FIRST[0]:
            case 'f':
            printf("%s == FIRST\n", argv[1]);
            break;
            case SECOND[0]:
            case 's':
            printf("%s == SECOND\n", argv[1]);
            break;
            }
            return 0;
            }
            >
            % gcc -o t -W -Wall -g t.c
            t.c: In function `main':
            t.c:10: error: case label does not reduce to an integer constant
            t.c:14: error: case label does not reduce to an integer constant
            >
            It certainly looks constant to me, and it should be promotable to an
            integer exactly as the neighboring case label is. This is gcc 3.4.5 FWIW.
            Possibly not allowed for the same reason that:

            int x[FIRST[0]];

            is not allowed, when declared outside a function (inside a function, it
            might work as a VLA, but it is still not considered a constant).

            I suppose there is a limit to what a compiler can do to determine an
            expression is constant, or maybe it considers that the string "first" could
            change (I managed to do this on lccwin, but gcc generated a crash when
            attempting to change the constant string).

            --
            Bart


            Comment

            Working...