I am surprised that a switch without the case is not flagged as an error:
enum // display modes for "Time lived";
{
TIME_ALL = 0, // show years, months, days, hours, minutes and seconds;
DAYS_ONLY,
HOURS_ONLY,
MINUTES_ONLY
};
int mode = TIME_ALL;
void setup()
{
}
void loop()
{
switch (mode)
{
TIME_ALL:
break;
DAYS_ONLY:
break;
HOURS_ONLY:
break;
MINUTES_ONLY:
break;
}
}
system
March 17, 2012, 8:08pm
2
I think all switches without an explicit 'default' case have an implicit 'default' case just before the closing brace.
Hmm - the C standard says it differently (6.8.4.2.5):
If no converted case constant expression matches and there is no default label, no part of the switch body is executed.
system
March 17, 2012, 9:56pm
3
I think you've missed the point - there are no "case"!
In effect, the statement reads something like
switch (x) {
0: break;
1: break;
}
I believe those end up as labels as used with 'goto'.
Yeap, this is it, they are considered labels (used for goto).
Thanks.
system
March 17, 2012, 10:15pm
6
Hah!
Correct, in which case, (no pun intended) ignore reply#2
I am still wondering if this is correct C or it should be flagged as an error ("missing case").
I just noticed, how weird, that I have no other C compiler on my machine (as I used to years ago).
Does anyone care to test it on Visual C++ for example, see if they treat it differently?
Thanks again.
One more observation:
switch (x) {
0: break;
1: break;
}
The code above, which seems equivalent, generates an error.
system
March 17, 2012, 11:36pm
9
Yes of course. It's ok to have no cases, but it's not ok to begin a label with a digit.
system
March 18, 2012, 4:55am
10
have i been typing out case every time for no reason at all... all... the... time...