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
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………..!!!!
You may like to read this also....
4 Comments to “Why variables can not be declared in a switch statement just after labels?”
Post comment
Search in this website
our sponsors
latest comments
- tammylleanne on Implementation limitation of c programming language
- Deepak on How to access/unblock songs.pk in india?
- venkat on C programming interview questions and answers for freshers
- Ankur on How to configure mail from localhost ( wamp ) using PHP?
- ergVFyd on Huffman Encoding Using Linked List with Source Code
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language


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.
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..!!
JIGAR PATEL recently posted..Whether should i use processes or threads in Linux?
Let me know how can I bookmark your website in my wordpress.com page?
Rasmiranjan Nayak recently posted..Volatile Keyword in Embedded System
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….
JIGAR PATEL recently posted..Whether should i use processes or threads in Linux?