home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 241,400 of 243,242   
   David Brown to Richard Harnden   
   Re: bugprone-switch-missing-default-case   
   22 Oct 25 15:41:55   
   
   From: david.brown@hesbynett.no   
      
   On 22/10/2025 13:44, Richard Harnden wrote:   
   > On 22/10/2025 10:32, Janis Papanagnou wrote:   
   >> On 22.10.2025 10:56, pozz wrote:   
   >>>   
   >>>> Switch statements without a default case can lead to unexpected   
   >>>> behavior and incomplete handling of all possible cases. When a switch   
   >>>> statement lacks a default case, if a value is encountered that does   
   >>>> not match any of the specified cases, the program will continue   
   >>>> execution without any defined behavior or handling.   
   >>>   
   >>> Maybe I misunderstood that sentence caused by my bad English. I knew   
   >>> that in case the switch value is not present in any case inside the   
   >>> switch, the program continues without doing anything (in the switch) and   
   >>> without any problem.   
   >>>   
   >>> int x = 3;   
   >>> switch(x) {   
   >>>    case 1: printf("Hello");break;   
   >>>    case 2: printf("World");break;   
   >>> }   
   >>>   
   >>> Will the program execution continue without any defined behaviour?   
      
   Presumably you meant "without any undefined behaviour" ?  The code is   
   fine - if no cases match and there is no default case, execution   
   continues from the end of the switch statement.  Like most warnings,   
   this is about a possible bug in the code - not a definite one.   
      
   >>   
   >> Your program fragment is well defined.   
   >>   
   >> What the poster certainly tried to express was that in case you   
   >> haven't implemented a complete list of all possible cases and   
   >> also not provided a 'default' to catch all non-specified cases,   
   >> then you might get in troubles with your program, probably by   
   >> possible oversights, future extensions, new data, and whatnot.   
   >>   
   >> Personally I have the habit to always define a default branch,   
   >> and even if that default is impossible to reach you'll find an   
   >> error message (like "internal error with unexpected value...")   
   >> generated at that place.   
      
   I don't think it is normally appropriate to add a default case unless   
   you actually need it - code that exists but can never be reached is   
   untestable and can be confusing to people reading the code.  But   
   sometimes it can be useful to add a "default : printf("Internal   
   error...");" for debugging, however.   
      
   In some code, I will have "default : __builtin_unreachable();" to   
   improve code efficiency.   
      
      
   >>   
   > Use an enum, and the compiler will warn you ...   
   >   
   > $ cat x.c   
   > #include    
   >   
   > enum x {A, B, C};   
   >   
   > int main(void)   
   > {   
   >      enum x x = C;   
   >   
   >      switch (x)   
   >      {   
   >          case A:   
   >              printf("A\n");   
   >              break;   
   >   
   >          case B:   
   >              printf("B\n");   
   >              break;   
   >      }   
   >   
   >      return 0;   
   > }   
   >   
   > $ gcc -Wall x.c   
   > x.c: In function ‘main’:   
   > x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch]   
   >      9 |         switch (x)   
   >        |         ^~~~~~   
   >   
   >   
      
   gcc has a number of warning options regarding switches :   
      
   -Wimplicit-fallthrough=n   
   Warns when you have fall-through between cases, unless you add a   
   [[fallthrough]] attribute, "// fallthrough" comment, or the like.  It is   
   enabled by -Wextra.   
      
   -Wswitch   
   Warns when you have a switch on an enumerated type, and are missing an   
   enumeration value and don't have a default case.  This is in -Wall.   
      
   -Wswitch-default   
   Warns when a switch does not have a default case.   
      
   -Wswitch-enum   
   Warns when you have a switch on an enumerated type, and are missing an   
   enumeration value, even if you have a default case.   
      
      
      
   I think it is generally a good idea to use enumerated types when you are   
   representing a number of possible options, rather than a plain "int"   
   with "magic numbers" or individual #define constants.  Then "-Wswitch"   
   (usually from "-Wall") or "-Wswitch-enum" is typically a good idea.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca