how can we use the changeable variable as a switch case label.
in other words,
I have a macro defined. But I need to change this value at run time depending on a condition. How can I implement this?
example is given below,
Here , case "FOO" will work?
#define CONDITION (strcmp(str, "hello") == 0)
#define FOO1 (10)
#define FOO2 (20)
#define FOO ((CONDITION) ? (FOO1) : (FOO2))
char *var="hello";
int main()
{
int p = 20;
switch(p) {
case FOO:
printf("\n case FOO");
break;
case 30:
printf("\n case 30");
break;
default:
printf("\n case default");
break;
}
return(0);
}
The switch condition needs to be resolved at compile-time. The case values need to be compile time constant expressions
From your question, you want to use run time condition to change the value of the case, so that is not possible.
One way to achieve run time check is to use if condition.
Your macro #define CONDITION (strcmp(str, "hello") == 0) isn't complete. It doesn't take in any argument.
The compiler will simply say str isn't defined in this scope.
Regardless, the case values are constants, so you won't be able to achieve this since your condition depends on run-time inputs.
It is important to know that most compilers implement the cases via a branch table. This is possible only because the case values are compile-time known (i.e. constants). The compiler will generate code to use your input as an index into this branch table to get to the logic for a particular case.
tl;dr - You can't use switch. Use if-elseif-else instead
Related
Through a little typo, I accidentally found this construct:
int main(void) {
char foo = 'c';
switch(foo)
{
printf("Cant Touch This\n"); // This line is Unreachable
case 'a': printf("A\n"); break;
case 'b': printf("B\n"); break;
case 'c': printf("C\n"); break;
case 'd': printf("D\n"); break;
}
return 0;
}
It seems that the printf at the top of the switch statement is valid, but also completely unreachable.
I got a clean compile, without even a warning about unreachable code, but this seems pointless.
Should a compiler flag this as unreachable code?
Does this serve any purpose at all?
Perhaps not the most useful, but not completely worthless. You may use it to declare a local variable available within switch scope.
switch (foo)
{
int i;
case 0:
i = 0;
//....
case 1:
i = 1;
//....
}
The standard (N1579 6.8.4.2/7) has the following sample:
EXAMPLE In the artificial program fragment
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.
P.S. BTW, the sample is not valid C++ code. In that case (N4140 6.7/3, emphasis mine):
A program that jumps90 from a point where a variable with automatic storage duration is not in scope to a
point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default
constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the
preceding types and is declared without an initializer (8.5).
90) The transfer from the condition of a switch statement to a case label is considered a jump in this respect.
So replacing int i = 4; with int i; makes it a valid C++.
Does this serve any purpose at all?
Yes. If instead of a statement, you put a declaration before the first label, this can make perfect sense:
switch (a) {
int i;
case 0:
i = f(); g(); h(i);
break;
case 1:
i = g(); f(); h(i);
break;
}
The rules for declarations and statements are shared for blocks in general, so it's the same rule that allows that that also allows statements there.
Worth mentioning as well is also that if the first statement is a loop construct, case labels may appear in the loop body:
switch (i) {
for (;;) {
f();
case 1:
g();
case 2:
if (h()) break;
}
}
Please don't write code like this if there is a more readable way of writing it, but it's perfectly valid, and the f() call is reachable.
There is a famous use of this called Duff's Device.
int n = (count+3)/4;
switch (count % 4) {
do {
case 0: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
Here we copy a buffer pointed to by from to a buffer pointed to by to. We copy count instances of data.
The do{}while() statement starts before the first case label, and the case labels are embedded within the do{}while().
This reduces the number of conditional branches at the end of the do{}while() loop encountered by roughly a factor of 4 (in this example; the constant can be tweaked to whatever value you want).
Now, optimizers can sometimes do this for you (especially if they are optimizing streaming/vectorized instructions), but without profile guided optimization they cannot know if you expect the loop to be large or not.
In general, variable declarations can occur there and be used in every case, but be out of scope after the switch ends. (note any initialization will be skipped)
In addition, control flow that isn't switch-specific can get you into that section of the switch block, as illustrated above, or with a goto.
Assuming you are using gcc on Linux, it would have given you a warning if you're using 4.4 or earlier version.
The -Wunreachable-code option was removed in gcc 4.4 onward.
Not only for variable declaration but advanced jumping as well. You can utilize it well if and only if you're not prone to spaghetti code.
int main()
{
int i = 1;
switch(i)
{
nocase:
printf("no case\n");
case 0: printf("0\n"); break;
case 1: printf("1\n"); goto nocase;
}
return 0;
}
Prints
1
no case
0 /* Notice how "0" prints even though i = 1 */
It should be noted that switch-case is one of the fastest control flow clauses. So it must be very flexible to the programmer, which sometimes involves cases like this.
It should be noted, that there are virtually no structural restrictions on the code within the switch statement, or on where the case *: labels are placed within this code*. This makes programming tricks like duff's device possible, one possible implementation of which looks like this:
int n = ...;
int iterations = n/8;
switch(n%8) {
while(iterations--) {
sum += *ptr++;
case 7: sum += *ptr++;
case 6: sum += *ptr++;
case 5: sum += *ptr++;
case 4: sum += *ptr++;
case 3: sum += *ptr++;
case 2: sum += *ptr++;
case 1: sum += *ptr++;
case 0: ;
}
}
You see, the code between the switch(n%8) { and the case 7: label is definitely reachable...
* As supercat thankfully pointed out in a comment: Since C99, neither a goto nor a label (be it a case *: label or not) may appear within the scope of a declaration that contains a VLA declaration. So it's not correct to say that there are no structural restrictions on the placement of the case *: labels. However, duff's device predates the C99 standard, and it does not depend on VLA's anyway. Nevertheless, I felt compelled to insert a "virtually" into my first sentence due to this.
You got your answer related to the required gcc option -Wswitch-unreachable to generate the warning, this answer is to elaborate on the usability / worthyness part.
Quoting straight out of C11, chapter §6.8.4.2, (emphasis mine)
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.
Which is very self-explanatory. You can use this to define a locally scoped variable which is available only within the switch statement scope.
It is possible to implement a "loop and a half" with it, although it might not be the best way to do it:
char password[100];
switch(0) do
{
printf("Invalid password, try again.\n");
default:
read_password(password, sizeof(password));
} while (!is_valid_password(password));
Hello I've been sitting here for quite some time trying to figure out why this isn't working but I haven't had luck succeeding!
#include <stdio.h>
main(void)
{
int mark;
printf("Please enter the mark\n");
scanf("%d", &mark);
switch(mark){
case (mark <40):
printf("That's a fail");
break;
case (mark >=40 && <60):
printf("That's a pass");
break;
case (mark >=60 && <70):
printf("That's a merit");
break;
case (mark >=70):
printf("That's a distinction");
break;
}
}
For comparisons where the exact value is not known, use if-else blocks. You are trying to use a switch statement like an if-else block, and it does not work properly. A proper switch would look like
switch(mark) {
case 40: //if mark is 40, no more, no less
...
case 60: //same
...
}
Because you don't want to write out 40 lines that all lead to the same statement, just use
if(mark < 40) { ... }
else if(mark < 60) { ... }
From K&R:
"The switch statement is a mult-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly."
That is, you can't do what you're trying to do. Each case needs a constant statement, such a simple int or char value.
For what you want to do you'll need if/else. Also, see jaap's answer on correct syntax for your boolean statement.
It's working fine, the compiler is preventing you from compiling something that's not valid C.
The case labels must be actual integer constants, not expressions that do further calculations.
You should use if/else if to check your various conditions.
This question already has answers here:
Variable definition inside switch statement
(5 answers)
Closed 5 years ago.
Code:
int main()
{
int a=1;
switch(a)
{
int b=20;
case 1:
printf("b is %d\n",b);
break;
default:
printf("b is %d\n",b);
break;
}
return 0;
}
Output:
It prints some garbage value for b
when does the declaration of b takes place here
Why b is not initialized with 20 here???
Because memory will be allocated for int b but when the application is run "b = 20" will never be evaluated.
This is because your switch-statement will jump down to either case 1:1 or default:, skipping the statement in question - thus b will be uninitialized and undefined behavior is invoked.
The following two questions (with their accepted answers) will be of even further aid in your quest searching for answers:
How can a variable be used when it's definition is bypassed? 2
Why can't variables be declared in a switch statement?
Turning your compiler warnings/errors to a higher level will hopefully provide you with this information when trying to compile your source.
Below is what gcc says about the matter;
foo.cpp:6:10: error: jump to case label [-fpermissive]
foo.cpp:5:9: error: crosses initialization of 'int b'
1 since int a will always be 1 (one) it will always jump here.
2 most relevant out of the two links, answered by me.
Switch statements only evaluate portions of the code inside them, and you can't put code at the top and expect it to get evaluated by every case component. You need to put the b initialization higher in the program above the switch statement. If you really need to do it locally, do it in a separate set of braces:
Code:
int main()
{
int a=1;
/* other stuff */
{
int b=20;
switch(a)
{
case 1:
printf("b is %d\n",b);
break;
default:
printf("b is %d\n",b);
break;
}
}
/* other stuff... */
return 0;
}
The switch directly jumps to case 1:, never executing the assignment.
Presumably because switch functions like a goto - if a == 1, it jumps straight to case 1: and bypasses initialization of b.
That is: I know switch jumps straight to the case label, but I'm very surprised the compiler doesn't complain about the missed initialization.
It's a pretty bad idea to initialize B under the switch statement and outside a case statement. To understand what's going on here, you have to know that the switch makes a jump to the correct case/default statement.
because when switch(a) statement executes control goes directly to the statement case 1: without executing statement int b=20, taht's why it gives garbage value as answer. If u want to print a then either u have to initialise in case 1: block or u have to initialise to before switch(a) statement.
Because that line is never reached. When C hits a switch(a) statement, it branches to the case that matches the condition of the variable you're switching on. The statement that initialises b isn't in any of the cases. I suppose the compiler is free to write 20 into the location, but the language doesn't require it to do so and in this case it doesn't: it's also free to leave initialisation until it actually executes an assignment.
Normally when using a switch statement, you cannot define and initialize variables local to the compound statement, like
switch (a)
{
int b = 5; /* Initialization is skipped, no matter what a is */
case 1:
/* Do something */
break;
default:
/* Do something */
break;
}
However, since the switch statement is a statement like for or while, there is no rule against not using a compound statement, look here for examples. But this would mean, that a label may be used between the closing parenthesis after the switch keyword and the opening brace.
So in my opinion, it would be possible and allowed to use a switch statement like this:
switch (a)
default:
{
int b = 5; /* Is the initialization skipped when a != 1? */
/* Do something for the default case using 'b' */
break;
case 1: // if a == 1, then the initialization of b is skipped.
/* Do something */
break;
}
My question: Is the initialization necessarily performed in this case (a != 1)? From what I know of the standards, yes, it should be, but I cannot find it directly in any of the documents I have available. Can anyone provide a conclusive answer?
And before I get comments to that effect, yes, I know this is not a way to program in the real world. But, as always, I'm interested in the boundaries of the language specification. I'd never tolerate such a style in my programming team!
Most people think of a switch as a mutiple if, but it is technically a calculated goto. And the case <cte>: and default: are actually labels. So the rules of goto apply in these cases.
Your both your examples are syntactically legal, but in the second one, when a==1 the b initialization will be skipped and its value will be undefined. No problem as long as you don't use it.
REFERENCE:
According to C99 standard, 6.2.4.5, regarding automatic variables:
If an initialization is specified for the object, it is performed each time the declaration is reached in the execution of the block;
So the variable is initialized each time the execution flow reaches the initialization, just as it were an assignment. And if you jump over the initialization the first time, then the variable is left uninitialized.
int a = 10;
switch(a){
case 0:
printf("case 0");
break;
case 1:
printf("case 1");
break;
}
Is the above code valid?
If I am sure that int a will not have any other value than 1 and 0, can I avoid default?
What if in any case a value will be different from 1 and 0?
I know this is a silly question but I was thinking that perhaps it would be illegal or undefined behavior soI just asked to make sure.
The code is valid. If there is no default: label and none of the case labels match the "switched" value, then none of the controlled compound statement will be executed. Execution will continue from the end of the switch statement.
ISO/IEC 9899:1999, section 6.8.4.2:
[...] If no converted case constant expression matches and there is no default label, no part of the switch body is executed.
As others have pointed out it is perfectly valid code. However, from a coding style perspective I prefer adding an empty default statement with a comment to make clear that I didn't unintentionally forget about it.
int a=10;
switch(a)
{
case 0: printf("case 0");
break;
case 1: printf("case 1");
break;
default: // do nothing;
break;
}
The code generated with / without the default should be identical.
It is perfectly legal code. If a is neither 0 or 1, then the switch block will be entirely skipped.
It's valid not to have a default case.
However, even if you are sure that you will not have any value rather than 1 and 0, it's a good practice to have a default case, to catch any other value (although it is theoretically impossible, it may appear in some circumstances, like buffer overflow) and print an error.
Default is not mandatory, but it always good to have it.
The code is ideally, but our life is not, and there isn't any harm in putting in a protection there. It will also help you debugging if any unexpected thing happens.
Yes, the above code is valid.
If the switch condition doesn't match any condition of the case and a default is not present, the program execution goes ahead, exiting from the switch without doing anything.
It's same like no if condition is matched and else is not provided.
default is not an mandatory in switch case. If no cases are matched and default is not provided, just nothing will be executed.
The syntax for a switch statement in C programming language is as follows:
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}