Thread: switch () {default: <something>; case ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    switch () {default: <something>; case ...

    Consider the following:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            int i = 3;
    
            switch (i){
                    default:
                            i = -1;
                            break;
                    case 1:
                    case 2:
                    case 3:
                            i = 5;
                            break;
            }
    
            printf ("i = %i.\n", i);
            return 0;
    }
    Output is:
    i = 5.

    Is this a definition of the standard, or is this compiler specific (ie, gcc). This code example is one that I tried and it works as if the default was at the end, but I would think it would match the default FIRST and never test against 1,2,3.

    The code that coRnfused me was from the Linux kernel in the file kernel/posix-cpu-timers.c.

    Thanks in advance for your answers.

    Andy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kennedy
    Is this a definition of the standard, or is this compiler specific (ie, gcc).
    Well...
    Quote Originally Posted by C99 Clause 6.8.4.2 Paragraph 5 (part)
    If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement.
    Hence the behaviour follows that described by the standard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hmm, so then everyone that I polled here believed it to be a syntax error, however, it worked, based upon the standard. So, this leads me to believe that near everyone is taught that default must come at the end, which is erroneous.

    Yet again, I have found something I was taught to be fact as false.

    Bummer man.

    Thanks laserlight!

    Andy

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It's only stylistic convention that places it at the end.

    For extra fun, mis-spell it as defualt and see how long it takes people to spot it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Hmm, so then everyone that I polled here believed it to be a syntax error
    Better not show them Duff's device then, they'd all explode!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  4. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  5. Intel syntax on MinGW ?
    By TmX in forum Tech Board
    Replies: 2
    Last Post: 01-06-2007, 09:44 AM