Re: c symbols with different meanings in different contexts



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.

.



Relevant Pages

  • Re: Almost!! Adding an ABOUT dialog
    ... I am not sure I understand your comments on the comma though! ... I never went back to using commas in declaration lists. ... the confusion of assiging 0 ...
    (microsoft.public.vc.mfc)
  • Re: Problem with reading an int with operator>>
    ... int n, m; ... if the two ints are separated by a comma. ... with the subject locale and streams on VC8 ... so that I dont use the precompiled dll? ...
    (microsoft.public.vc.stl)
  • Re: Need help converting c code to something Borland can handle
    ... >Borland is saying it expects a comma on the first typedef. ... The product you are using has an extended integer type called __int64. ... sees unsigned it looks ahead for the name of an integer type (char, int, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: How to read data ?
    ... ranjeet.gupta@gmail.com wrote: ... A less trusting program is safer. ... > int main{ ... forgets a comma - or even intentionally leaves one out to break your ...
    (comp.lang.c)
  • Re: Text Substitution Spanning Multiple Lines
    ... I'm trying to find the right tool to do text substitutions over ... PYWKORDER int, ... All that is left to do is to get rid of the final trailing comma in the ... And as far as I can tell both sed and awk only ...
    (comp.unix.shell)