Thread: Switch statement clarification

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    Switch statement clarification

    I tried to write this error code. The first printf is not executed as expected. Is it removed from the code as a part of optimization?
    Code:
    #include <stdio.h>
    int main(void)
    {
     int i=12;
     switch(i)
     {
       printf("Switch Error Condition\n");
       case 1:
        printf("In 1\n");
        break;
       default:
        break;
     }
    return 0;
    }

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    That's not how the switch statement is supposed to work (what did you expect for the 1st printf? )

    You should either put it under a case/default clause or completely outside the switch statement (most probably above it, if I judge correctly what you are trying to do).
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I thought compiler should have shown an error or warning.

  4. #4
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Me too, tbh (haven't checked, I take your word for it - perhaps there are some kind of flexibility I'm not aware of). However, that 1st printf() seems quite out of place anyway.
    "Talk is cheap, show me the code" - Linus Torvalds

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    733
    Turn the compiler warning levels up to high when you compile and warning should show up. No access to a compiler right now to test.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Having code before the first case of a "switch()" is allowed, but not very useful.

    Execution jumps straight to the appropriate case:

    Quote Originally Posted by c99-6.8.4.2
    5 The integer promotions are performed on the controlling expression. The constant
    expression in each case label is converted to the promoted type of the controlling
    expression. 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. If no converted case constant
    expression matches and there is no default label, no part of the switch body is
    executed
    .
    Actually, there's an example of this concept in the very next section of the standard:

    Quote Originally Posted by c99
    Code:
    switch (expr)
    {
        int i = 4;
        f(i);
    case 0:
        i = 17;
        /* falls through into default code */
    default:
        printf("%d\n", i);
    }
    the object whose identifier is i exists with automatic storage duration (within the block) but is never
    initialized, and thus if the controlling expression has a nonzero value, the call to the printf function will
    access an indeterminate value. Similarly, the call to the function f cannot be reached.
    I don't think compilers are required to generate a warning when code is skipped in this way, though it's possible some compilers might.

  7. #7
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Just checked on Windows:
    - mingw/mingw-w64 & tiny-c do not issue any warnings
    - pelles-c & lcc-win32 issue a "unreachable code" warning

    PS. Frankly I do not see why it is even allowed, since it's totally ignored.
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'If' Statement inside the Switch statement being ignored.
    By howardbc14 in forum C Programming
    Replies: 4
    Last Post: 04-11-2015, 11:45 AM
  2. Switch Statement Help
    By ShiroAzure in forum C++ Programming
    Replies: 2
    Last Post: 09-08-2011, 08:40 PM
  3. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  4. Switch statement help
    By GeorgeV in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2007, 03:35 AM
  5. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 07:16 AM