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,409 of 243,242   
   Kaz Kylheku to Thiago Adams   
   Re: bugprone-switch-missing-default-case   
   22 Oct 25 18:22:50   
   
   From: 643-408-1753@kylheku.com   
      
   On 2025-10-22, Thiago Adams  wrote:   
   > On 10/22/2025 8:44 AM, Richard Harnden wrote:   
   > ....   
   >>> 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.   
   >>>   
   >> 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)   
   >>        |         ^~~~~~   
   >>   
   >>   
   >   
   > The problem with this GCC approach is when there are many enumerators   
   > but only a few are used.   
      
   The problem with the C and GCC approach is that there is no   
   one-size-fits all solution.   
      
   Some switches are intended to be exhaustive, such that   
   missing a case is a bug.   
      
   Some are not.   
      
   You need an "eswitch" for the exhaustively handled enumerations,  and   
   switch for the others.   
      
   GCC can turn on diagnostics over ranges of a file with pragma   
   and there is also _Pragram, but it's all too clumsy.   
      
   In Common Lisp we have case vs ecase:   
      
   [1]> (case 3 (0 "zero") (1 "one") (2 "two"))   
   NIL   
   [2]> (case 0 (0 "zero") (1 "one") (2 "two"))   
   "zero"   
   [3]> (ecase 3 (0 "zero") (1 "one") (2 "two"))   
      
   *** - The value of 3 must be one of 0, 1, 2   
         The value is: 3   
      
   If falling through without hitting a case would be wrong, you use ecase;   
   problem solved.   
      
   --   
   TXR Programming Language: http://nongnu.org/txr   
   Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal   
   Mastodon: @Kazinator@mstdn.ca   
      
   --- 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