Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch
statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch
statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
Your program fragment is well defined.Use an enum, and the compiler will warn you ...
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch]
9 | switch (x)
| ^~~~~~
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch
statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
Use an enum, and the compiler will warn you ...
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch]
9 | switch (x)
| ^~~~~~
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:Use an enum, and the compiler will warn you ...
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch
statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch]
9 | switch (x)
| ^~~~~~
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>> statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch)
and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
I don't think it is normally appropriate to add a default case unless
you actually need it
- code that exists but can never be reached is
untestable and can be confusing to people reading the code. But
sometimes it can be useful to add a "default : printf("Internal
error...");" for debugging, however.
On 22.10.2025 15:41, David Brown wrote:
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>>> statement lacks a default case, if a value is encountered that does >>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>> that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) >>>>> and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
I don't think it is normally appropriate to add a default case unless
you actually need it
Yes I was saying that I want it; I "need" it once errors slip in and
such a message or error log immediately clears the issue! (You might
be excluding some "needs" from your repertoire of necessities, okay.)
- code that exists but can never be reached is
untestable and can be confusing to people reading the code. But
sometimes it can be useful to add a "default : printf("Internal
error...");" for debugging, however.
This printf error message or log entry is what I suggested. It isn't confusing because it even _documents_ what's the case here. Rather,
a missing default leaves the reader with an unnecessary uncertainty.
YMMV.
On 22.10.2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:Use an enum, and the compiler will warn you ...
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>> statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
Maybe. Although I don't recall that the "C"-compilers I formerly
used checked enums as you've shown below. - But anyway...
Enums don't help if you use and compare against (for example)
characters that are (for example) got from an external source.
char * cmds = "CMDQ"; // 'D' maybe added later
char cmd;
...some n lines of code...
...get input cmd...
...optionally verify cmd with cmds...
...some more m lines of code...
switch (cmd) {
case 'C': ...;
case 'M': ...;
default: printf ("Error: uncaught cmd '%c'\n", cmd);
}
It's good to take precautions and define the 'default' case. YMMV.
From here[1]:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch
statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English.
On 10/22/2025 8:44 AM, Richard Harnden wrote:
....
Your program fragment is well defined.Use an enum, and the compiler will warn you ...
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch] >> 9 | switch (x)
| ^~~~~~
The problem with this GCC approach is when there are many enumerators
but only a few are used.
(case 3 (0 "zero") (1 "one") (2 "two"))NIL
(case 0 (0 "zero") (1 "one") (2 "two"))"zero"
(ecase 3 (0 "zero") (1 "one") (2 "two"))
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>> statement lacks a default case, if a value is encountered that does
not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew
that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
On 22/10/2025 15:56, Janis Papanagnou wrote:
On 22.10.2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:Use an enum, and the compiler will warn you ...
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>>> statement lacks a default case, if a value is encountered that does >>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>> that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the
switch) and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
Maybe. Although I don't recall that the "C"-compilers I formerly
used checked enums as you've shown below. - But anyway...
Enums don't help if you use and compare against (for example)
characters that are (for example) got from an external source.
char * cmds = "CMDQ"; // 'D' maybe added later
char cmd;
...some n lines of code...
...get input cmd...
...optionally verify cmd with cmds...
...some more m lines of code...
switch (cmd) {
case 'C': ...;
case 'M': ...;
default: printf ("Error: uncaught cmd '%c'\n", cmd);
}
It's good to take precautions and define the 'default' case. YMMV.
That's not "taking precautions". If the "...optionally verify cmd" part
does a good job, then the default line is worse than useless because it
is code that never runs. [...]
On 22/10/2025 16:05, Janis Papanagnou wrote:
On 22.10.2025 15:41, David Brown wrote:
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a
switch
statement lacks a default case, if a value is encountered that does >>>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>>> that in case the switch value is not present in any case inside the >>>>>> switch, the program continues without doing anything (in the switch) >>>>>> and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
I don't think it is normally appropriate to add a default case unless
you actually need it
Yes I was saying that I want it; I "need" it once errors slip in and
such a message or error log immediately clears the issue! (You might
be excluding some "needs" from your repertoire of necessities, okay.)
- code that exists but can never be reached is
untestable and can be confusing to people reading the code. But
sometimes it can be useful to add a "default : printf("Internal
error...");" for debugging, however.
This printf error message or log entry is what I suggested. It isn't
confusing because it even _documents_ what's the case here. Rather,
a missing default leaves the reader with an unnecessary uncertainty.
YMMV.
Indeed YMMV. But when I see a printf call that says something has gone horribly wrong,
I wonder /how/ that could happen.
I don't put such
statements into my code without it being a possible execution path.
I
agree, of course, that a good error message here makes the code somewhat self-documenting in terms of what it does.
But it does nothing to help say how it happened to run.
A printf call that never runs is not free -
it costs in many different ways, and should not be there unless it is
worth those costs.
David Brown <david.brown@hesbynett.no> writes:
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>>> statement lacks a default case, if a value is encountered that does >>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>> that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>>>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
No, pozz meant "without any defined behavior", which is a direct quote
from the cited document.
That document is poorly written. The phrase "without any defined
behavior" strongly implies that the behavior is undefined, which is
simply wrong.
It says:
When a switch statement lacks a default case, if a value is
encountered that does not match any of the specified cases, the
program will continue execution without any defined behavior or
handling.
It would be more accurate to say:
When a switch statement lacks a default case, if a value is
encountered that does not match any of the specified cases, the
switch statement will do nothing and the program will continue
execution without handling the value.
A warning might be warranted, but the behavior is well defined.
Note that the documentation is for the add-on tool clang-tidy, not for
the clang compiler.
I've submitted a bug report :
https://github.com/llvm/llvm-project/issues/164699
On 22.10.2025 17:25, David Brown wrote:
On 22/10/2025 15:56, Janis Papanagnou wrote:
On 22.10.2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:Use an enum, and the compiler will warn you ...
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>>>> statement lacks a default case, if a value is encountered that does >>>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>>> that in case the switch value is not present in any case inside the >>>>>> switch, the program continues without doing anything (in the
switch) and
without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Your program fragment is well defined.
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
Maybe. Although I don't recall that the "C"-compilers I formerly
used checked enums as you've shown below. - But anyway...
Enums don't help if you use and compare against (for example)
characters that are (for example) got from an external source.
char * cmds = "CMDQ"; // 'D' maybe added later
char cmd;
...some n lines of code...
...get input cmd...
...optionally verify cmd with cmds...
...some more m lines of code...
switch (cmd) {
case 'C': ...;
case 'M': ...;
default: printf ("Error: uncaught cmd '%c'\n", cmd);
}
It's good to take precautions and define the 'default' case. YMMV.
That's not "taking precautions". If the "...optionally verify cmd" part
does a good job, then the default line is worse than useless because it
is code that never runs. [...]
Not sure what you expect in that line. I've had something like
strchr (cmds, cmd) in mind. And the 'switch' is independent
of that, so you could miss adding the switch branch of a later
added command character.
The point was that mistakes can be made, not only in the initial implementation but also if it gets extended later, and probably
by other folks than the original implementer. I also gave a hint
with "...some more m lines of code..." that such omissions may
also be hard to spot.
It is experience from professional real life projects that such
things happen. And blaming anyone that he's not done "a good job"
may be the appropriate diagnosis and important point if what you
want is primarily to blame someone, but if you want software to
get developed reliably, one element is to quickly spot the place
of such omissions.
On 2025-10-22, Thiago Adams <thiago.adams@gmail.com> wrote:
On 10/22/2025 8:44 AM, Richard Harnden wrote:
....
Your program fragment is well defined.Use an enum, and the compiler will warn you ...
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-Wswitch]
9 | switch (x)
| ^~~~~~
The problem with this GCC approach is when there are many enumerators
but only a few are used.
The problem with the C and GCC approach is that there is no
one-size-fits all solution.
Some switches are intended to be exhaustive, such that
missing a case is a bug.
Some are not.
You need an "eswitch" for the exhaustively handled enumerations, and
switch for the others.
GCC can turn on diagnostics over ranges of a file with pragma
and there is also _Pragram, but it's all too clumsy.
On 22.10.2025 17:23, David Brown wrote:
On 22/10/2025 16:05, Janis Papanagnou wrote:
But it does nothing to help say how it happened to run.
??? - The example scenario will run. Just may have erroneous results
when triggering the case that isn't handled. With diagnostic records
you can quickly identify and fix it (usually in one of the QA test
cycles before you deliver the software).
A printf call that never runs is not free -
it costs in many different ways, and should not be there unless it is
worth those costs.
(You're obviously a better discussion candidate for folks who count
bytes and microseconds, say, like bart. - The "costs" that matters
much more in professional software development are quality and time.)
David Brown <david.brown@hesbynett.no> writes:
On 22/10/2025 13:44, Richard Harnden wrote:
On 22/10/2025 10:32, Janis Papanagnou wrote:
On 22.10.2025 10:56, pozz wrote:
Switch statements without a default case can lead to unexpected
behavior and incomplete handling of all possible cases. When a switch >>>>>> statement lacks a default case, if a value is encountered that does >>>>>> not match any of the specified cases, the program will continue
execution without any defined behavior or handling.
Maybe I misunderstood that sentence caused by my bad English. I knew >>>>> that in case the switch value is not present in any case inside the
switch, the program continues without doing anything (in the switch) and >>>>> without any problem.
int x = 3;
switch(x) {
case 1: printf("Hello");break;
case 2: printf("World");break;
}
Will the program execution continue without any defined behaviour?
Presumably you meant "without any undefined behaviour" ? The code is
fine - if no cases match and there is no default case, execution
continues from the end of the switch statement. Like most warnings,
this is about a possible bug in the code - not a definite one.
No, pozz meant "without any defined behavior", which is a direct quote
from the cited document.
That document is poorly written. The phrase "without any defined
behavior" strongly implies that the behavior is undefined, which is
simply wrong.
It says:
When a switch statement lacks a default case, if a value is
encountered that does not match any of the specified cases, the
program will continue execution without any defined behavior or
handling.
It would be more accurate to say:
When a switch statement lacks a default case, if a value is
encountered that does not match any of the specified cases, the
switch statement will do nothing and the program will continue
execution without handling the value.
A warning might be warranted, but the behavior is well defined.
Note that the documentation is for the add-on tool clang-tidy, not for
the clang compiler.
I've submitted a bug report :
https://github.com/llvm/llvm-project/issues/164699
On 22/10/2025 20:22, Kaz Kylheku wrote:
On 2025-10-22, Thiago Adams <thiago.adams@gmail.com> wrote:
On 10/22/2025 8:44 AM, Richard Harnden wrote:
....
Your program fragment is well defined.Use an enum, and the compiler will warn you ...
What the poster certainly tried to express was that in case you
haven't implemented a complete list of all possible cases and
also not provided a 'default' to catch all non-specified cases,
then you might get in troubles with your program, probably by
possible oversights, future extensions, new data, and whatnot.
Personally I have the habit to always define a default branch,
and even if that default is impossible to reach you'll find an
error message (like "internal error with unexpected value...")
generated at that place.
$ cat x.c
#include <stdio.h>
enum x {A, B, C};
int main(void)
{
enum x x = C;
switch (x)
{
case A:
printf("A\n");
break;
case B:
printf("B\n");
break;
}
return 0;
}
$ gcc -Wall x.c
x.c: In function ‘main’:
x.c:9:9: warning: enumeration value ‘C’ not handled in switch [-
Wswitch]
9 | switch (x)
| ^~~~~~
The problem with this GCC approach is when there are many enumerators
but only a few are used.
The problem with the C and GCC approach is that there is no
one-size-fits all solution.
Some switches are intended to be exhaustive, such that
missing a case is a bug.
Some are not.
You need an "eswitch" for the exhaustively handled enumerations, and
switch for the others.
GCC can turn on diagnostics over ranges of a file with pragma
and there is also _Pragram, but it's all too clumsy.
The gcc approach works fine in almost all situations - use "- Wswitch=error", and add a default case if your switch is not meant to
handle all enumeration values. If the default should do nothing, it's
just "default: // Not all cases need handling". If the default should never happen, "default: __builtin_unreachable();" or "default: __builtin_trap();" might be appropriate.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,073 |
Nodes: | 10 (0 / 10) |
Uptime: | 222:34:40 |
Calls: | 13,783 |
Calls today: | 1 |
Files: | 186,987 |
D/L today: |
702 files (242M bytes) |
Messages: | 2,434,871 |