C, labels, scope
newton at cit-vax.ARPA
newton at cit-vax.ARPA
Sat Jul 28 04:46:33 AEST 1984
From: Mike Newton <newton at cit-vax.ARPA>
A friend is in the muddle of writing C compiler ... He came up with the
following oddity:
----------------
the question has to do with the scoping of statement labels. the appropriate
lines in the appendix to k&r read as follows:
(from section 9.12, p204)
"The scope of a label is the current function,excluding any sub-blocks
in which the same identifier has been re-declared."
(from section 11.1, p206)
"The lexical scope of labels is the whole of the function in which they
appear."
this gives a clear interpretation to such code as
f()
{
int j;
goto i;
{
int i;
i = j;
}
i: ;
}
the case becomes more sticky, however, if the the scope of the variable
becomes larger; consider, for example, the following two cases:
g()
{
int i;
{
i: ;
}
i++;
}
h(i)
int i;
{
i++;
i: ;
}
... and, to further complicate the situation, suppose the label is declared
in the same block as the variable ... well, you get the idea. in trying
to determine the correct interpretation of these constructions, i tried
feeding them to various different c compilers ... and got almost as many
different error messages as i had compilers.
the real question here is just this: what, exactly, is the scope of a
label -- or, in the terms of the line from k&r, what, exactly, is the
"whole of the function"?? i see two possibilities:
1. the "whole of the function" includes the formal parameters, the
function header, and everything else associated withthe definition of
the function. in this case, labels would be treated as having the same
scope as formal parameters; and, like formal parameters, they could be
redeclared in the outermost block of the function. thus, function g()
above would be legal; the legality (and, if it is legal, the meaning) of
function h() above is dubious.
2. the "whole of the function" includes only the compound statement
which contains the actual code for the function. in this case, function
h() above might be legal -- the label being treated as a redeclaration
of the name in an inner block, having the scope of the inner block --
and the legality and possible meaning of function g() becomes dubious.
(personally, i tend towards interpretation 1. most c compilers i've
seen tend toward interpretation 2, although none is clear or consistent
in either direction.)
the problems of the scoping of labels are further illustrated by the
following piece of code:
foo()
{
goto i;
{
int i;
i: i = 2;
}
}
one possible interpretation of the quote in k&r is that this is, in
fact, legal c !! consider that the scope of the label is the whole
of the function in which it is declared (making the "goto i" legal)
minus any sub-blocks in which the name is re-declared (making the "i=2"
legal).
so what i want to know is what's right here, and what's wrong?? am i
missing something obvious?? which (if any) of these are legal?? (or,
in other words, would someone please tell me what's going on, and which
way is up??)
please reply by mail to me, and not to the above address; my site
doesn't get this group, and a friend is posting this for me. thanks.
fritz.
ucbvax!cithep!fritz
More information about the Comp.lang.c
mailing list