Thread: c switch declaration

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    50

    c switch declaration

    Code:
    #include<stdio.h>
    main() 
    { switch (i) 
    { case 1: 
    int a = 3;
     break; 
    case 2:printf("hello"); break; } }
    Code:
    #include<stdio.h>
    main()
    {switch (i)      
    {int a = 3;        
    case 1:printf("%d",a);         
     break;       
     case 2:printf("hello");     
       break;     
     }   
     }
    I know that the first code will produce an error..But why does in the second code a gets declared even though it is inside switch and in the first code it doesnot even declare it??

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Really both are probably incorrect. But the first will compile if you add braces to your case statement:
    Code:
    switch (i)
    { 
       case 1:
       {
          int a = 3;
          // Do other things.
       }
       break;
       case 2:
          printf("hello"); 
          break; 
       } 
    }
    But remember that a will only be declared within that first case statement, not in any of the others. Also in the second snippet a is only available inside the switch statement, nowhere else.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    50
    @jimblumberg Thank you for your answer..but my question is that why the declaration is allowed inside switch block and not allowed inside case statement...although incase of inside switch you can have variable declared but not initialized..but why is this not possible in case of case statement??

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The case statement without the braces doesn't create it's own scope which is required in order to declare a variable. I really don't know why you're allowed to create the variable inside the switch statement and not get either an error or warning message.

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Both examples are incorrect for the same reason. They permit jumping over initialisation of the variable a.

    This is related to the fact that case statements are, in effect, just labels within the scope that contains the switch statement, and the switch statement does not introduce an additional scope. In both cases, the definition of a is of a variable within that enclosing scope as well.

    It is illegal to jump to a label if doing so would bypass the initialisation of a variable. In this sense, the switch statement is a glorified goto, as it is in effect a jump statement. In the first example, jumping to case 2 will bypass initialisation of a. In the second example, jumping to either case will bypass initialisation of a.

    Different compilers do complain about this in different ways. For example, some compilers deem that it is illegal to have a label that is not part of a statement (since a variable declaration is not formally a statement).


    In the above I'm ignoring the fact that the variable i is used but never declared. That is ALSO incorrect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. declaration
    By Tool in forum C Programming
    Replies: 3
    Last Post: 01-17-2010, 10:18 AM
  2. declaration
    By valthyx in forum C Programming
    Replies: 7
    Last Post: 07-28-2009, 04:11 PM
  3. A switch that doesn't switch
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 11-06-2004, 12:18 PM
  4. Declaration
    By forge in forum Windows Programming
    Replies: 3
    Last Post: 11-28-2001, 06:17 PM
  5. declaration
    By Clay in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-13-2001, 10:20 AM

Tags for this Thread