Jan
28

Why variables can not be declared in a switch statement just after labels?

see this code

#include<stdio.h>
int main()
{
   int i=1;
   switch(i)
   {
       case 1: int x=10;
               printf(" x is %d",x);
       break;
    }
    return 0;
}

Do you think this code will compile? if yes then you need to read this post….

see the compile snapshot of that code http://ideone.com/qZVbj

that code will not be compile and get error

prog.c: In function ‘main’:
prog.c:8: error: a label can only be part of a statement and a declaration is not a statement
Case statements are only 'labels'

In c according to spec

§6.8.1 Labeled Statements:


labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

In c there is no any clause that allows for a “labeled declaration”. It’s just not part of the language.

So

case 1: int x=10;
        printf(" x is %d",x);
break;

so this will not compile see http://codepad.org/YiyLQTYw gcc is giving error

label can only be a part of statement and declaration is not a statement 

even

case 1: int x;
                      x=10;
                      printf(" x is %d",x);
              break;

this is also not compile see http://codepad.org/BXnRD3bu here also i am getting same error.


In C++ according to spec

labeled-declaration is allowed but labeled -initialization is not allowed.

see this

http://codepad.org/ZmQ0IyDG


Solution to such condition is three ways

1> Either use new scope using {}

Case statements are only ‘labels’. This means the compiler will interpret this as a jump directly to the label.The problem here is one of scope. Your curly brackets define the scope as everything inside the ‘switch’ statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization. The correct way to handle this is to define a scope specific to that case statement and define your variable within it.

case 1:
       {
        int x=10;
        printf(" x is %d",x);
       }
break;

2> or use dummy statement with label

case 1: ;
        int x=10;
        printf(" x is %d",x);
break;

3> declare variable before switch() and initilize it with different values in case statement if it fulfill your requirement

main()
{
int x;   // declare before
switch(a)
{
case 1: x=10;
break;
case 2: x=20;
break;
}
}

some more things with switch statement

newer writes any statements in switch which are not part of any label. because they will never executed .
NOTE: declaration can be written there but not statement (int i; is declaration but int i = 10; is declaration + assignment = statement so assignment will not be perform there..!)

switch(a)
{
printf("This will never print"); // this will never executed
case 1:
        printf(" 1");
break;
 
default :
break;
}

see this http://codepad.org/PA1quYX3

This is what i understood after my graduation completes………..!!!!

 

JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More PostsWebsiteTwitterFacebook

You may like to read this also....

4 Comments to “Why variables can not be declared in a switch statement just after labels?”

  • Rasmiranjan Nayak February 1, 2012 at 5:55 am

    Hi JIGAR PATEL,
    You wrote a fantastic article.
    I think the same question I have faced in Wipro interview around more than a year back.
    This article really helps…
    Thanks…. And keep writing some good articles.And update me when you are going to post a new article.

    • JIGAR PATEL February 2, 2012 at 1:31 pm

      Thanks a lot for such valuable comment….:)

      yea i dont like to make post by just copy paste from other sites. I Just write my experience , my learning …every day we learn/take much knowledge from INTERNET so here i am just giving something back to INTERNET…

      well till yet i havnt find one proper wordpress plugin for taking email id of interested people and informing those by sending email of new post , but i will do that soon…till that please follow me on facebook or twitter i usually share links of my new post..!!

  • Rasmiranjan Nayak February 1, 2012 at 5:58 am

    Let me know how can I bookmark your website in my wordpress.com page?

    • JIGAR PATEL February 2, 2012 at 1:36 pm

      In my wordpress dashboard i have option for Links at left side of dashboard

      Inside that Under Link categories i have added one new categories as “MY BOOKMARKS” then by clicking “Add new” i add new link there…

      ( NOTE : Inside Link by default one categories is available that is “blogroll” )

      still if you dont getting then please let me know….

Post comment

*

Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

36 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link