Standard on switch() 
Author Message
 Standard on switch()

Lately I've had to use Microsoft's Visual C++, and I just
noticed this.  In the following switch statement:

    switch( someint )
    {
        case 0:
            break;
        case 1:
            break;
        default:
    }

MSVC6 gives an error "syntax error : missing ';' before '}'".
It does not like the last label, be it default: or not, with
no C statement under it.  Testing this on gcc 2.95, it compiles
without any warnings and the -Wall option specified.

Which makes me wonder what does the standard say on this.  Are
"empty last labels" prohibited?

P.S.  I know there's no need for an "empty last label", such
as in the example above, because we'll exit the switch anyways.
The code above is just an example.  I actually found this by
commenting out all the code below the last case in switch().

--
Alex Georgiev
--



Wed, 06 Feb 2002 03:00:00 GMT  
 Standard on switch()

   Lately I've had to use Microsoft's Visual C++, and I just
   noticed this.  In the following switch statement:

       switch( someint )
       {
           case 0:
               break;
           case 1:
               break;
           default:
       }

   MSVC6 gives an error "syntax error : missing ';' before '}'".
   It does not like the last label, be it default: or not, with
   no C statement under it.

According to the Standard, every label must be followed by a
statement, even if that's just the empty statement ';'.

   Testing this on gcc 2.95, it compiles without any warnings and the
   -Wall option specified.

If you specify -ansi -pedantic, gcc 2.95 (and earlier versions) will
complain, too.

   Which makes me wonder what does the standard say on this.  Are
   "empty last labels" prohibited?

Yes.
--
"Debian for hackers, Red Hat for suits, Slackware for loons."
--CmdrTaco <URL:http://slashdot.org/articles/99/03/22/0928207.shtml>
--



Wed, 06 Feb 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. : Switch default back to switch?

2. Standard C using Standard C

3. redirecting standard output and standard error

4. C standard draft VS C standard

5. coding anti-anti-standards [was: coding anti-standards]

6. Any documentation for CL.EXE /Bt /Bd switches? Any other /B switches available?

7. Where does MFC switch menu when No MDI is present? How to prevent that from switching?

8. C# language design flaw (IMHO) switch/case fall through

9. The #1 Reason to Switch from VB to C#

10. Switch Statement

11. VC++.NET (2002):Fatal Error C1189-Please use the /MD switch for _AFXDLL builds

12. Documentation for link /IGNORE switch?

 

 
Powered by phpBB® Forum Software