#include<stdio.h>
void main()
{
int a = 4;
switch (a)
{
case 4:
int res = 1;
printf("%d",res);
break;
}
}
When I compiled this code with gcc I got error
root@ubuntu:/home/ubuntu# gcc test.c -o t
test.c: In function ‘main’:
test.c:9:4: error: a label can only be part of a statement and a declaration is not a statement
int res = 1;
But when I add ;
like case 4:;
I can compile my code.
What is the problem and why ;
fix that?
Declaring a variable inside a switch block is fine. Declaring after a case guard is not.
In switch case we can use only integer and character type data types. And that is case of character only single character is allowed. The switch case converts each character into its ascii value and hence compares them. That is why a string cannot be checked in the switch case.
The switch statement doesn't accept arguments of type long, float, double,boolean or any object besides String.
It is okay to declare variables with same name in different functions. Show activity on this post. Variables declared inside a function only exist in the scope of that function, so having the same variable name across different functions will not break anything.
The entire section of the switch is a single declaration context. You can't declare a variable in a case statement like that. Try this instead: Show activity on this post. If your code says "int newVal=42" then you would reasonably expect that newVal is never uninitialised.
Declare the variable before switch () and initialize it with different values in case statement if it fulfills your requirement Never write any statements in the switch which are not part of any label, because they will never executed:
A switch construct creates a whole, first-class-citizen scope. So it is posible to declare (and initialize) a variable in a switch statement before the first case label, without an additional bracket pair: Show activity on this post.
You can still declare variables in switch statements, you just have to put curly brackets around the code after the case label. Compare the two examples below. The first one generates an error.
Opposite to C++, in C declarations are not statements and labels may precede only statements.
Placing a null statement after the label
case 4:;
you made that the label does not precede now a declaration.
Another approach is to use a compound statement after the label and to place the declaration inside the compound statement like
case 4:
{
int res = 1;
printf("%d",res);
break;
}
The problem is in your error message: labels can only be attached to statements and a declaration is not a statement.
Seeing N1570 6.8.2 Compound statement, there are declaration
and statement
enumerated separately, and it is suggesting that they are different things.
Syntax 1 compound-statement: { block-item-listopt } block-item-list: block-item block-item-list block-item block-item: declaration statement
The ;
in case 4:;
is a null statement (defined in N1570 6.8.3 Expression and null statements), which is a statement, and thus it is allowed.
What is the problem and why ; fix that?
The problem is just what the error message says. Your case 4:
is a label. Labels identify the immediately-following statement, and therefore must immediately precede a statement. Declarations such as int res = 1;
are not statements.
On the other hand, adding a semicolon creates an empty statement, which, as its name implies, is a statement, and therefore can be labeled. That's why it solves the problem.
With that said, I'd rate the semicolon solution poor style. I'd also rate declaring a variable whose enclosing block is the body of a switch
as poor style, and that one can bite you in other ways, too. Consider instead moving the declaration of res
outside the switch
...
int a = 4;
int res = 1;
switch (a) {
case 4:
printf("%d",res);
break;
}
... or enclosing it in an inner block (which is a compound statement, and therefore can be labeled) ...
int a = 4;
switch (a) {
case 4: {
int res = 1;
printf("%d",res);
break;
}
}
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With