Section 6.8.1 of C11 or C99, or section 3.6.1 of C89 all seem to indicate that default
and case x
(where x
is some constant-expression) are examples of labeled statements, along-side identifier:
-style labels that are suitable for use with goto
.
I'm aware that I could simply place an identifier:
-style label directly following the default:
or case x:
labels. That's not what this question is about. I'm more curious as to whether there is any actual rationale behind prohibiting this kind of behaviour.
If it were possible to declare default:
labels outside of a switch
selection structure, then I would understand, as there would be some conflict between where the goto
inside of the switch
selection structure is intended to aim. However, section 6.4.1 of C11 or C99 or 3.1.1 of C89 prohibits the use of default
as anything other than a keyword, and 6.8.1 restricts its use further to switch
structures only (or generic
structures in C11, which are irrelevant here).
I would also understand if multiple (possibly nested) switch
structures, each with default:
(or case x:
) labels introduced ambiguity, however the scope of those labels seems to be restricted to within their inner-most surrounding switch
structures, and referring to any identifier outside of its scope is clearly an error requiring a diagnostic at compile-time.
Has this been discussed in any standard documents (e.g. the rationale)? Is there any kind of explanation for this behaviour other than "it is because it is" or "because the spec says so"? If so, what is that explanation?
case
,default
labels are not identifiers, named labels."Because the spec says so." Do not overthink it.