Like Tree1Likes
  • 1 Post By Bayint Naung

Enumeration values not handled

This is a discussion on Enumeration values not handled within the C Programming forums, part of the General Programming Boards category; Hi, all. I'm compiling with gcc -Wall -Wextra. My functions return an enumeration to the caller, and then the caller ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Enumeration values not handled

    Hi, all. I'm compiling with gcc -Wall -Wextra. My functions return an enumeration to the caller, and then the caller deals with it like this:

    Code:
    switch (result)
    {
    case success:
       /*blah*/
    case failure:
      /*blah*/
    case another:
      /*blah*/
    }
    Now, my switch statements do not handle all possible values within the enumeration, because I know that it is (or should be) impossible for certain values to be returned in certain circumstances. GCC gives me:

    warning: enumeration value 'another' not handled in switch
    warning: control reaches end of non-void function

    Now whatever code GCC is generating for the other values of the switch, I know it can never be executed. What's the 'proper' way of dealing with this?

    Richard (TIA)

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    warning: control reaches end of non-void function
    That's pretty self-explanatory. You have a function that is supposed to return value. but you are not returning anything.


    warning: enumeration value 'another' not handled in switch
    IBM TPF Product Information Center
    Daniel Brockman - Re: enumeration value ... not handled in switch

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    126

    Not possible

    But, I'm saying, every possible code path returns a value. The compiler is wrong to think that control could ever reach the closing curly brace. It hasn't understood the code well enough... or alternatively, if control ever could reach the closing curly brace, then I stuffed up somewhere.

    Richard

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    typedef enum { apple,orange, banana } fruit;
    
    int main() {
      fruit f = orange;
      switch(f) {
       case apple : printf("hello world\n");
       return 0;
       case orange : printf("it's orange \n");
       return 0;
       }
     // unless you have return 0; here. 
    }
    $ gcc -Wall -Wextra hello.c
    hello.c: In function ‘main’:
    hello.c:7:3: warning: enumeration value ‘banana’ not handled in switch
    hello.c:14:1: warning: control reaches end of non-void function
    If f is not apple or orange, Then control falls through(no default case) and compiler is right to say warning: control reaches end of non-void function.
    Edit: you could add default: case to make compiler happy.
    Last edited by Bayint Naung; 03-20-2011 at 10:31 PM.
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Free program I'm sharing: ConvertEnumToStrings
    By Programmer_P in forum Projects and Job Recruitment
    Replies: 101
    Last Post: 07-18-2010, 01:55 AM
  2. scanf return values
    By pirog in forum C Programming
    Replies: 15
    Last Post: 09-13-2009, 04:58 AM
  3. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  4. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-31-2009, 12:53 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 08:49 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21