Re: c symbols with different meanings in different contexts
- From: jameskuyper <jameskuyper@xxxxxxxxxxx>
- Date: Wed, 6 May 2009 14:28:11 -0700 (PDT)
amit.codename13@xxxxxxxxx wrote:
in the statement
int a=1,2;
i get the following error with a gcc compiler
/home/amit/main.c||In function ‘main’:|
/home/amit/main.c|6|error: expected identifier or ‘(’ before numeric
constant|
||=== Build finished: 1 errors, 0 warnings ===|
the error suggests that either we put a identifier after the comma :
int a=1,b;
or we put a '(' before numeric constant :
int a=(1,2);
so one makes comma act as a initializer list separator and the other
makes it act as a comma operator...
so what may be actual reason for generation of the error...
You'll have to ask the programmer who generated the error what the
reason was. :-)
does it violate the constraints in the syntax of a comma operator or
in the syntax of declarations...
The reason for the generation of the error message (as opposed to the
reason for the generation of the error itself) is as follows:
The syntax of initializers requires that they be either a single
assignment-expression, or a comma-delimited list of initializers
enclosed in curly brackets: {} (6.7.8p1). In this context, 1,2 cannot
be parsed as an assignment-expression using the comma operator,
because the result of applying a comma operator is a plain expression,
not an assignment expression (6.5.17p1). Only by putting it in
parethesis would it qualify as a primary expression (6.5.1p1), and
therefore (6.5.2 through 6.5.15) as an assignment expression.
Therefore, when the parser reaches the ',' it assumes that the
initializer for 'a' is complete, and treats the ',' as indicating that
the next thing it will see is a new init-declarator (6.7p1). When it
finds the '2', it identifies that a syntax error of some kind has
occurred. As Keith explained, it has no way of being certain what kind
of error was committed, so all it can do is make suggestions for
improvement.
.
- References:
- c symbols with different meanings in different contexts
- From: amit.codename13@xxxxxxxxx
- c symbols with different meanings in different contexts
- Prev by Date: Re: c symbols with different meanings in different contexts
- Next by Date: Re: c symbols with different meanings in different contexts
- Previous by thread: Re: c symbols with different meanings in different contexts
- Next by thread: Macro expansion surprise.
- Index(es):
Relevant Pages
|