I came across the following line of code, and I can't figure out what it does.
#define static_assert(a, b) do { switch (0) case 0: case (a): ; } while (0)
What does the switch (0) part do? Assuming 0 is equivalent to false, does that mean we never enter the switch statement?
Also for the line case (a), how can you give the unknown a variable as a case?
switch(0) will always execute the block of code associated with the case 0: block; still, here there's no actually executed code - both cases are empty.
The point here is to make the compiler angry at compile time if the asserted expression (a) is not verified: in this case, the expanded macro will have two case 0: - the one provided explicitly, and the one that uses the result of the asserted expression (so, 0 in case it failed); this results in a switch with two identical case, which is not allowed and makes the compiler stop with an error at compile time.
This will also fail if the passed expression is not a constant evaluated at compile time (as you cannot have runtime-determined case values), which is also expected from a static_assert.
Related
I recently came across the code snippet shown below, I was expecting it to be a syntax error but to my surprise, the code produces valid output.
#include <stdio.h>
int main(void) {
int x = 2;
switch(x) {
case 1: printf("1"); break;
do {
case 2: printf("2 "); break;
case 3: printf("3 "); break;
} while(++x < 4);
case 4: printf("4"); break;
}
return 0;
}
output: 2 4
Compiler: GCC 6.3
I found a similar problem but it is not justifying above condition completely,
Mixed 'switch' and 'while' in C
Can anyone explain,
What exactly happening here?
Why isn't it a syntax error?
Why case '3' is skipped?
case X: some_statement; is a labeled statement (6.8.1) just like goto_label: some_statement; with the only caveat that case/default labels may only appear inside the body of a switch (possibly in an arbitrarily nested compound statement). That makes case statements only very loosely coupled with switches, syntactically.
Semantically, switches are implementable as computed gotos and like regular gotos, they may jump pretty much anywhere (in C11, you can't jump past a VLA declaration) including inside of a loop (see https://en.wikipedia.org/wiki/Duff%27s_device#Mechanism for another description).
In your example, case 3: is skipped because of the break, but case 4: does follow because the break after case 3: is a loop-breaking break, not a switch-breaking break (break/continue always apply to the nearest construct they can apply to).
The official C grammar for a switch statement is:
switch ( expression ) statement
Any statement can be a labeled-statement, for which the grammar includes:
case constant-expression : statement
This means you can put pretty much whatever you want inside the body of the switch: It can be a compound statement which includes multiple statements and a do-while statement that includes multiple statements, and case labels can be prefixed to any of those statements.
The compiler merely implements the switch using jump instructions (inside its abstract machine; they may end up as other instructions after optimization). Loops statements such as do-while are implement with code that tests the controlling expression and jumps conditionally. So, in spite of the nice structure you think of in structured languages, it boils down to jump instructions, and those can be interwoven as desired.
The jumps are not the most disconcerting part of this. Object initialization and lifetime is more of a concern. Switch statements can unintentionally skip the initialization of objects if care is not taken.
The printf of “3 ” is never executed because control jumps from the switch to case 2, then breaks, which exits the do-while. This leaves code at the case 4 statement, which prints “4 ” and then breaks out of the switch statement.
Given this code:
#include <stdio.h>
int main( int argc, char** argv )
{
switch (0) {
case 1: printf("1");
case 2: printf("2");
default:
printf("default");
break;
}
return 0;
}
I'd expect that the compiler would tell me something about either conditional expression is constant (switch (0) while VS2012 issues this warning for an if(0)) or unreachable code (case 1, case 2).
However, neither on ideone nor on Visual Studio 2012 nor on recent GCCs I receive anything like that.
Why don't even decent compilers complain here?
I just tried it on CLANG with extended warning turned on and received the following warning:
Referring to (oddly enough) only the the first of the two lines in the switch that will never be executed:
case 1: printf("1");
From the C 2011 standard:
5.1.1.3 Diagnostics
1 A conforming implementation shall produce at least one diagnostic message (identified in
an implementation-defined manner) if a preprocessing translation unit or translation unit
contains a violation of any syntax rule or constraint, even if the behavior is also explicitly
specified as undefined or implementation-defined. Diagnostic messages need not be
produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each
violation. Of course, an implementation is free to produce any number of diagnostics as long as a
valid program is still correctly translated. It may also successfully translate an invalid program.
Emphasis added.
So, a diagnostic is required if using a constant expression in a switch or if control expression is a constraint violation...
6.8.4.1 The if statement
Constraints
1 The controlling expression of an if statement shall have scalar type.
...
6.8.4.2 The switch statement
Constraints
1 The controlling expression of a switch statement shall have integer type.
2 If a switch statement has an associated case or default label within the scope of an
identifier with a variably modified type, the entire switch statement shall be within the
scope of that identifier.154)
3 The expression of each case label shall be an integer constant expression and no two of
the case constant expressions in the same switch statement shall have the same value
after conversion. There may be at most one default label in a switch statement.
(Any enclosed switch statement may have a default label or case constant
expressions with values that duplicate case constant expressions in the enclosing
switch statement.)
154) That is, the declaration either precedes the switch statement, or it follows the last case or
default label associated with the switch that is in the block containing the declaration.
...which it isn't.
This is basically a quality of implementation issue; the implementation may issue a diagnostic for using a constant expression in an if or switch statement, but it doesn't have to. if( 0 ) and switch( 0 ) may strike most of us as being hinky, but the implementation is not required to issue any diagnostics over them.
To get the warning in Visual Studio 2013 you need:
right click on the project --> properties
C/C++ in the section General set Warning level to ALL
The warning is only for the if statement because the switch is a block.
I just tried clang -Weverything: it complains about argc and argv not being used, but stays silent about switch(0), just like gcc.
While it is not mandatory for the compiler to issue a diagnostic, a warning might help if the error is a typo. Try this one for example:
int test(int l) {
switch (1) {
case 0: return 0;
case 1: return 1;
default: return -1;
}
You should file a bug on both projects.
EDIT: clang seems to have fixed this recently, unlike what ryyker shows in his response, -Wunreachable-code does not complain on my laptop. I might have an older version.
I'm reading KN King's A Modern Approach to C Programming, 2nd edition.
It says, there are also other forms of switch statement besides general switch statement (with case keyword).
The general form of switch statement is
switch (exp)
{
case constant-exp:
statement;
break;
case constant-exp:
statement;
break;
...
...
default:
statement;
break;
}
It also says (in Q&A) switch statement can have form with no case keyword for example.
I tried running an example with no case keyword, but it doesn't run (under std=-c99).
So, I wanted to know what are the other forms of switch statement that are valid in Standard C99.
EDIT: Cited fro BOOK
In it's most common form, the switch statement has the form
switch ( expression ) {
case constant-expression : statements
...
case constant-expression : statements
default : statements
}
Q&A
**Q: The template given for the switch statement described it as the "most common form." Are there other forms?
A**: The switch statement is a bit more general than described in this chapter, although the description given here is general enough for virtually all programs.
For example, a switch statement can contain labels that aren't preceded by the word case, which leads to amusing (?) trap. Suppose that we accidentally missell the word default:
switch(...) {
...
defualt: ...
}
The compiler may not detect the error, since it assumes that defualt is an ordinary label.
The syntax of a switch statement is:
switch ( expression ) statement
where the statement portion is typically a block (compound statement) containing labeled statements of the form:
case constant-expression : statement
or
default : statement
A switch statement isn't required to contain case or default labels, but there's no point in using a switch if you're not going to have one or more such labels. For example, this:
switch (42);
is a perfectly legal switch statement (the controlled statement is the null statement ;), but it's also perfectly useless.
I suspect you've misunderstood what the book says.
Your quote from the book says:
For example, a switch statement can contain labels that aren't
preceded by the word case, which leads to amusing (?) trap. Suppose
that we accidentally mispsell the word default:
switch(...) { ... defualt: ... }
The contents of a switch statement should be a block containing a sequence of case and default labels, each one ending either with a break or with a comment indicating that the control flow falls through to the next case. The point is that the language doesn't require this; the way it specifies the syntax gives you a lot of freedom (perhaps too much!). The only restriction is that case and default labels cannot appear outside a switch statement.
For example, suppose you accidentally write:
enum blah { foo, bar, baz };
switch (expr) {
case foo:
/* ... */
break;
bar: /* forgot the `case` keyword */
/* ... */
break;
defualt: /* misspelled "default" */
/* ... */
break;
}
Neither bar: nor defualt: was what was intended -- but they're both perfectly legal. They're ordinary labels, the kind that can be the target of a goto statement. Since there is no goto targeting either label, the corresponding chunks of code will never be executed. If expr is equal to foo, it will jump to the case foo:; for any other value, it will jump to the end of the switch statement.
And because they're perfectly legal, a compiler won't necessarily warn you about the error.
This is a common phenomenon in C. The grammar is so "dense" that a seemingly minor typo can easily give you something that's syntactically valid, but whose behavior is entirely different from what you intended.
Crank up the warning levels on your compiler, and pay attention to all the warnings you see. And be careful; the responsibility for writing your code correctly is ultimately yours. The compiler can help, but it can't catch all errors.
Regarding your observation: there are also other forms of switch statement besides general switch statement (with case keyword) Generally, the switch statement is very well documented, but there are a few interesting variations in the way the case statements are used...
Although nothing Earth shaking here, It may be useful to note: Sun (a flavor of unix) and GNU C compiler have an extension that provides case ranges for use with the switch() statement. So, for example, rather than using the classic syntax:
:
case 'A':
case 'B':
:
case 'Z':
//do something here.
break;
and so on...
A case range syntax can be used to delineate the conditions:
switch(input) {
case 'A' ... 'Z':
printf("Upper case letter detected");
break;
case 'a' ... 'z':
printf("Lower case letter has been detected");
break;
};
Important Note:, case ranges are not part of the C standard (C99 or C11) rather only an extension of the environments I have mentioned, and in no way should be considered portable. Case ranges are gaining in popularity (or at least in interest) and may be included as part of the C standard at some point, but not yet (AFAIK).
The go-to source for C-99 is the C-99 standard, though of course C-99 has been replaced by C11. The switch statement is on page 134 of the C-99 standard. They give an example of what is probably the most non-general switch statement you can have:
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.
Note the ways that this is not "standard" (and is generally bad code).
You have code that is not under any case or default label. In fact, nothing but identifier declarations seems to be acknowledged, so that i is a valid variable within the scope of the switch statement, but setting it or calling functions without a case or default label causes errors.
What I think the author wanted you to notice was that it's valid to not have a break under each label. In this example, case 0 falls through to the default label, but if there were other case labels beneath case 0, it would go through each one until you did hit a break statement.
For that matter, though not in this example, you can put the default label first. Again, if you don't put a break after it, you'll execute code under any following labels.
As I mentioned in my comment, you could just have a default label if you want, but that effectively renders the switch statement meaningless:
switch (exp)
{
default:
statement;
}
that's effectively equivalent to { statement; }.
Incidentally, you can do some clever (but confusing) tricks with avoiding break statements, e.g. this is a valid (though less efficient than c - '0') way to convert a digit character c to an integer:
int i = 0;
switch (c) {
case '9': ++i;
case '8': ++i;
case '7': ++i;
case '6': ++i;
case '5': ++i;
case '4': ++i;
case '3': ++i;
case '2': ++i;
case '1': ++i;
case '0':
default:
}
Wiki describes how the switch statement defined as below considering different coding languages,
In most languages, a switch statement is defined across many individual lines using one or two keywords. A typical syntax is:
1. The first line contains the basic keyword, usually switch, case or select,
followed by an expression which is often referred to as the control expression
or control variable of the switch statement.
2. Subsequent lines define the actual cases (the values) with corresponding
sequences of statements that should be executed when a match occurs.
Each alternative begins with the particular value, or list of values, that the control variable may match and which will cause the control to go to the corresponding sequence of statements. The value (or list/range of values) is usually separated from the corresponding statement sequence by a colon or an implication arrow. In many languages, every case must also be preceded by a keyword such as case or when. An optional default case is typically also allowed, specified by a default or else keyword; this is executed when none of the other cases matches the control expression.
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
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.