i have a question about C89's "declarations before code" policy. (i'm
not challenging the policy, just trying to understand that to my eyes
appears to be an an ambiguity in the definition of "code".)
void foo()
{
int x = bar();
int y;
... do something demonstrative ...
}
what i'm trying to understand is why the call to bar() is _not_
considered "code", i.e. why it's allowed (at least using gcc's -
std=c89 and -pedantic flags, which AFAIK is strict-conformance mode),
whereas the following is not (in strict C89 mode):
void foo()
{
int x;
x = foo();
int y;
...
}
In the latter case gcc says "ISO C90 forbids mixed declarations and
code". In my eyes, the former is _semantically_ equivalent, but
obviously there's a technical difference i'm not clear on.
Yes, i understand that C99 allows this, and i don't mind [all that
much] that C89 doesn't. i'm just trying to better understand the C89
definition of "code" and why "declaration with assignment" is not
considered "code" for this purpose
:-?
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Yes, the call to bar() can reasonably be called "code", which is why the
use of the word "code" is a poor way to express the rule. The actual
rule in C90 is that declarations must precede *statements*. This is
expressed in the grammar for a compound statement:
compound-statement
{ declaration-list[opt] statement-list[opt] }
(the standard uses subscripts, which I've represented with square
brackets).
The C99 grammar for a compound statement is:
compound-statement:
{ block-item-list[opt] }
where a block-item is either a declaration or a statement.
But the C99 standard does refer to "mixed declarations and code" in the
foreword. "mixed declarations and statements" would have been clearer.
We just have to understand that the word "code", in this context, refers
only to statements. On the other hand, there are several examples in
the standard that clearly use the word "code" to refer to any C source,
including directives and declarations.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
The difference is that:
int x = foo();
int y;
is a declaration that happens to include an initializer, followed by another
declaration (without an initializer). Whereas:
int x;
x = foo();
int y;
is a declaration without an initializer, followed by "code", followed by
another declaration.
Subtle, yes, but the point is that the initializer does not count as "code"
as far as this is concerned.
--
Kenneth Brody
Thanks to both of you, Keith and Kenneth - your combined efforts have
clarified it for me.
Happy Hacking!
Thanks to both of you, Keith and Kenneth - your combined efforts have
clarified it for me.
Happy Hacking!