Problem with #define'd macro...

Christopher R Volpe volpe at camelback.crd.ge.com
Fri Apr 5 03:34:55 AEST 1991


In article <10903 at ncar.ucar.edu>, steve at groucho.ucar.edu (Steve
Emmerson) writes:
|>Whoops!  Hang on a second!
|>
|>I said the expression was valid according to the K&R1 grammar.  It is,
|>however, quite invalid under Standard C: it cannot be generated from
|>the grammar.
|>
|>The original question was in the context of K&R1 C.

Ah, right. Sorry. Yes, it's valid under K&R1, and SunOS cc incorrectly
flags it. Gcc -traditional also does the same incorrect thing. 
The default gcc (i.e. ansi) correctly flags it, but for the wrong reason.
According to ANSI it's a pure syntax violation, but gcc reports
"invalid lvalue in assignment". But that's because the "then" branch
of the conditional isn't an lvalue. If we take advantage of GCC's
generalized lvalues, we get the following:

#include <stdio.h>
int main()
{
  int a=0,b=0,c=0;
  c=0;
/*
  This get's reported as invalid lhs of assignment
  c ? c = a : c = b ;
*/

/*
  This is a prefectly valid generalized lvalue
*/
  (c ? a : b) = 5;

/*
  This is a syntax error according to ANSI, but GCC treats it like the above
*/
  c ? a : b = 5;

}

GCC behaves the same way with -ansi. However, it produces the 
"invalid lhs" again when using -pedantic. It never finds the syntax
violation.

|>
|>Steve Emmerson        steve at unidata.ucar.edu        ...!ncar!unidata!steve
                            
==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com



More information about the Comp.lang.c mailing list