Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
* comma operator: C compiler bug or bogus usage?
@ 1999-04-15  4:00 John Hascall
  1999-04-15  4:00 ` Noone Really
                   ` (5 more replies)
  0 siblings, 6 replies; 11+ messages in thread
From: John Hascall @ 1999-04-15  4:00 UTC (permalink / raw)


# uname -a
OSF1 pvtest.cc.iastate.edu V4.0 878 alpha

# cat ct.c
#include <stdio.h>

int foo[(3,4)];
int bar[3,4];

main(){ printf("sizes of foo and bar are %d, %d\n", sizeof(foo), sizeof(bar)); }

# cc  -g -O0 ct.c  -o ct
cc: Error: ct.c, line 4: Missing "]".
int bar[3,4];
---------^
cc: Error: ct.c, line 3: In the declaration of "foo", "3,4" is not constant, but occurs in a context that requires a constant expression.
int foo[(3,4)];
---------^

It seems to me that the first error message is clearly not helpful,
but what haven't been able to discover is:

   Is the result of:

          constant-expression comma-operator constant-expression

   still a constant-expression or not?  If not, why not (and it
   seems to me this is the only operator with this property?)

John
PS, FWIW, gcc accepts both of these and produces the 'expected' results
-- 
John Hascall, Software Engr.      Shut up, be happy.  The conveniences you
ISU Computation Center            demanded are now mandatory. -Jello Biafra
mailto:john@iastate.edu
http://www.cc.iastate.edu/staff/systems/john/index.html  <=- the usual crud



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
                   ` (3 preceding siblings ...)
  1999-04-15  4:00 ` Joerg Czeranski
@ 1999-04-15  4:00 ` Lawrence Kirby
  1999-04-15  4:00 ` Venkat
  5 siblings, 0 replies; 11+ messages in thread
From: Lawrence Kirby @ 1999-04-15  4:00 UTC (permalink / raw)


In article <7f567v$sp1$1@news.iastate.edu>
           john@iastate.edu "John Hascall" writes:

...

>It seems to me that the first error message is clearly not helpful,
>but what haven't been able to discover is:
>
>   Is the result of:
>
>          constant-expression comma-operator constant-expression
>
>   still a constant-expression or not?  If not, why not (and it
>   seems to me this is the only operator with this property?)

The C standard says

"Constant expressions shall not contain assignment, increment, decrement
 function-call, or comma operators, except when they are contained within
 the operand of a sizeof operator."

>John
>PS, FWIW, gcc accepts both of these and produces the 'expected' results

By default the language that gcc compiles is not C, it is what is
loosely called "GNU C" which includes a numbver of extensions over
standard C. Use gcc -ansi -pedantic to make gcc an essentially standard C
compiler. If you do you should find that it warns about your code.

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
                   ` (4 preceding siblings ...)
  1999-04-15  4:00 ` Lawrence Kirby
@ 1999-04-15  4:00 ` Venkat
  5 siblings, 0 replies; 11+ messages in thread
From: Venkat @ 1999-04-15  4:00 UTC (permalink / raw)


I don't know what you are trying to do, but you need to declare
multi-dimensional arrays like this: bar[3][4];

John Hascall <john@iastate.edu> wrote in message
news:7f567v$sp1$1@news.iastate.edu...
> # uname -a
> OSF1 pvtest.cc.iastate.edu V4.0 878 alpha
>
> # cat ct.c
> #include <stdio.h>
>
> int foo[(3,4)];
> int bar[3,4];
>
> main(){ printf("sizes of foo and bar are %d, %d\n", sizeof(foo),
sizeof(bar)); }
>
> # cc  -g -O0 ct.c  -o ct
> cc: Error: ct.c, line 4: Missing "]".
> int bar[3,4];
> ---------^
> cc: Error: ct.c, line 3: In the declaration of "foo", "3,4" is not
constant, but occurs in a context that requires a constant expression.
> int foo[(3,4)];
> ---------^
>
> It seems to me that the first error message is clearly not helpful,
> but what haven't been able to discover is:
>
>    Is the result of:
>
>           constant-expression comma-operator constant-expression
>
>    still a constant-expression or not?  If not, why not (and it
>    seems to me this is the only operator with this property?)
>
> John
> PS, FWIW, gcc accepts both of these and produces the 'expected' results
> --
> John Hascall, Software Engr.      Shut up, be happy.  The conveniences you
> ISU Computation Center            demanded are now mandatory. -Jello
Biafra
> mailto:john@iastate.edu
> http://www.cc.iastate.edu/staff/systems/john/index.html  <=- the usual
crud





^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
  1999-04-15  4:00 ` Noone Really
  1999-04-15  4:00 ` Casper H.S. Dik - Network Security Engineer
@ 1999-04-15  4:00 ` Kaz Kylheku
  1999-04-15  4:00   ` John Hascall
  1999-04-15  4:00 ` Joerg Czeranski
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 11+ messages in thread
From: Kaz Kylheku @ 1999-04-15  4:00 UTC (permalink / raw)


On 15 Apr 1999 17:05:03 GMT, John Hascall <john@iastate.edu> wrote:
># uname -a
>OSF1 pvtest.cc.iastate.edu V4.0 878 alpha
>
># cat ct.c
>#include <stdio.h>
>
>int foo[(3,4)];
>int bar[3,4];

Both of these declare array objects with one dimension, having four
elements.

>main(){ printf("sizes of foo and bar are %d, %d\n", sizeof(foo), sizeof(bar)); }
>
># cc  -g -O0 ct.c  -o ct
>cc: Error: ct.c, line 4: Missing "]".

Ah, you are being bitten by the fact that 3, 4 is not a constant expression.
Though this is a strange way to report the required diagnostic. :)

The expressions which specify array dimensions must be constant.  Expressions
that have a comma operator in them are not considered constant expressions,
even though both of the constituents may be constant expressions.

>int bar[3,4];
>
>          constant-expression comma-operator constant-expression
>
>   still a constant-expression or not?  If not, why not (and it

It's certainly an expression with a value that can be determined at compile
time, no argument there.  However, that is not the same as a constant
expression. The latter is just a term that is arbitrarily defined in the C
standard. You have to apply that definition to determine whether something
is a constnat expression or not.

>John
>PS, FWIW, gcc accepts both of these and produces the 'expected' results

It does not if you tell it to be an ANSI C compiler witn -ansi -pedantic.

I tried it with egcs 2.91.57, which accepts it without the -pedantic
option, but produces the following warning if you turn on -pedantic:

	warning: ANSI C forbids variable-size array 'a'.

You have fallen into a common pitfall by assuming that your compiler is a C
compiler by default. Many compilers default to supporting their implementor's
extensions, with ANSI C support being a negative option. For example, the
Microsoft compiler supports transparent unions and // comments by
default.  GCC supports // comments, statement blocks that behave like
expressions, array sizes that are computed at run time, nested functions and
other goodies.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
  1999-04-15  4:00 ` Noone Really
@ 1999-04-15  4:00 ` Casper H.S. Dik - Network Security Engineer
  1999-04-15  4:00 ` Kaz Kylheku
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Casper H.S. Dik - Network Security Engineer @ 1999-04-15  4:00 UTC (permalink / raw)


[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]

john@iastate.edu (John Hascall) writes:

>   Is the result of:

>          constant-expression comma-operator constant-expression

>   still a constant-expression or not?  If not, why not (and it
>   seems to me this is the only operator with this property?)

No.

>John
>PS, FWIW, gcc accepts both of these and produces the 'expected' results


Well, if you pass -ansi -pedantic:

gcc -ansi -pedantic x.c
x.c:3: warning: ANSI C forbids variable-size array `foo'
x.c:3: variable-size type declared outside of any function
x.c:4: warning: ANSI C forbids variable-size array `bar'
x.c:4: variable-size type declared outside of any function


Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
                   ` (2 preceding siblings ...)
  1999-04-15  4:00 ` Kaz Kylheku
@ 1999-04-15  4:00 ` Joerg Czeranski
  1999-04-15  4:00   ` Will Rose
  1999-04-15  4:00 ` Lawrence Kirby
  1999-04-15  4:00 ` Venkat
  5 siblings, 1 reply; 11+ messages in thread
From: Joerg Czeranski @ 1999-04-15  4:00 UTC (permalink / raw)


John Hascall wrote in <7f567v$sp1$1@news.iastate.edu>:
> # uname -a
> OSF1 pvtest.cc.iastate.edu V4.0 878 alpha
> 
> # cat ct.c
> #include <stdio.h>
> 
> int foo[(3,4)];
> int bar[3,4];
> 
> main(){ printf("sizes of foo and bar are %d, %d\n", sizeof(foo), sizeof(bar)); }
> 
> # cc  -g -O0 ct.c  -o ct
> cc: Error: ct.c, line 4: Missing "]".
> int bar[3,4];
> ---------^
> cc: Error: ct.c, line 3: In the declaration of "foo", "3,4" is not constant, but occurs in a context that requires a constant expression.
> int foo[(3,4)];
> ---------^
> 
> It seems to me that the first error message is clearly not helpful,
> but what haven't been able to discover is:
> 
>    Is the result of:
> 
>           constant-expression comma-operator constant-expression
> 
>    still a constant-expression or not?  If not, why not (and it
>    seems to me this is the only operator with this property?)
> 
> John
> PS, FWIW, gcc accepts both of these and produces the 'expected' results

I just checked a draft of C9X (I don't have the current ANSI/ISO standard),
and it appears to me that the compiler is right:

"[ ]" contains an assignment-expression, not a general expression
(section 6.5.5: Declarators).  Therefor "int bar[3],..." is valid,
but "int bar[3,..." not, and it's not unreasonable for the compiler
to complain about the missing "]" that would enable it to continue
parsing.

And:
Constant expressions shall not contain comma operators (section 6.4:
Constant expressions).

(C9X allows variable-size arrays though, so "int foo[(3,4)]" should
then be ok.)

gcc has a different handling of "," and IMHO it violates the standard
in that respect.  Even gcc -ansi -pedantic doesn't emit a diagnostic
for this error (it violates the constraint in 6.3.16.1: Simple assignment,
referred to by 6.5.8: Initialization):

  char weekday[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };

(That's an actual example of something a friend who is just learning C
stumbled over.)

joerch



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 ` Kaz Kylheku
@ 1999-04-15  4:00   ` John Hascall
  1999-04-15  4:00     ` Kaz Kylheku
  1999-04-16  4:00     ` Lawrence Kirby
  0 siblings, 2 replies; 11+ messages in thread
From: John Hascall @ 1999-04-15  4:00 UTC (permalink / raw)


Kaz Kylheku <kaz@ashi.FootPrints.net> wrote:
}>int bar[3,4];
}>          constant-expression comma-operator constant-expression
}>   still a constant-expression or not?  If not, why not ...

}It's certainly an expression with a value that can be determined at compile
}time, no argument there.  However, that is not the same as a constant
}expression. The latter is just a term that is arbitrarily defined in the C
}standard. You have to apply that definition to determine whether something
}is a constnat expression or not.

     Ah, silly me, the Humpty-Dumpty privilege!

}>John
}>PS, FWIW, gcc accepts both of these and produces the 'expected' results
}It does not if you tell it to be an ANSI C compiler witn -ansi -pedantic.

   Actually I tried it with -ansi -Wall
   but the man page scared me away from -pedantic :)

}I tried it with egcs 2.91.57, which accepts it without the -pedantic
}option, but produces the following warning if you turn on -pedantic:
}
}	warning: ANSI C forbids variable-size array 'a'.
}
}You have fallen into a common pitfall by assuming that your compiler is a C
}compiler by default. Many compilers default to supporting their implementor's
}extensions, with ANSI C support being a negative option. For example, the
}Microsoft compiler supports transparent unions and // comments by
}default.  GCC supports // comments, statement blocks that behave like
}expressions, array sizes that are computed at run time, nested functions and
}other goodies.

     Heh, well any compiler that defaults to trigraphs off gets my cheers!

John
-- 
John Hascall, Software Engr.      Shut up, be happy.  The conveniences you
ISU Computation Center            demanded are now mandatory. -Jello Biafra
mailto:john@iastate.edu
http://www.cc.iastate.edu/staff/systems/john/index.html  <=- the usual crud



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00   ` John Hascall
@ 1999-04-15  4:00     ` Kaz Kylheku
  1999-04-16  4:00     ` Lawrence Kirby
  1 sibling, 0 replies; 11+ messages in thread
From: Kaz Kylheku @ 1999-04-15  4:00 UTC (permalink / raw)


On 15 Apr 1999 19:51:17 GMT, John Hascall <john@iastate.edu> wrote:
>Kaz Kylheku <kaz@ashi.FootPrints.net> wrote:
>}>int bar[3,4];
>}>          constant-expression comma-operator constant-expression
>}>   still a constant-expression or not?  If not, why not ...
>
>}It's certainly an expression with a value that can be determined at compile
>}time, no argument there.  However, that is not the same as a constant
>}expression. The latter is just a term that is arbitrarily defined in the C
>}standard. You have to apply that definition to determine whether something
>}is a constnat expression or not.
>
>     Ah, silly me, the Humpty-Dumpty privilege!
>
>}>John
>}>PS, FWIW, gcc accepts both of these and produces the 'expected' results
>}It does not if you tell it to be an ANSI C compiler witn -ansi -pedantic.
>
>   Actually I tried it with -ansi -Wall
>   but the man page scared me away from -pedantic :)

The -ansi option just means to disable GCC extensions that would cause ansi
programs to be rejected or diagnosed. The -pedantic option is needed to further
comply with ANSI C by generating required diagnostics for syntax and constraint
violations.

That (outdated) man page is just GNU propaganda: ``there is no reason to use
this option; it exists only to satisfy pedants''. This is, of course, nonsense.
The list of syntax and constraint violations required by ANSI C is not all that
extensive; to say that only a pedant would want these diagnostics is laughable.

If your compiler doesn't diagnose an ANSI C syntax or constraint violation, the
code may well be rejected by another compiler. Or the lack of diagnosis may
even cause a defect to go undetected.

You see, the FSF guys want everyone to write in GNU C, that is, using the GCC
extensions, so that the code won't compile with those evil non-freeware
compilers that nobody should be using now that there is GCC. :)



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 ` Joerg Czeranski
@ 1999-04-15  4:00   ` Will Rose
  0 siblings, 0 replies; 11+ messages in thread
From: Will Rose @ 1999-04-15  4:00 UTC (permalink / raw)


Joerg Czeranski (jc@joerch.org) wrote:
[...]
: gcc has a different handling of "," and IMHO it violates the standard
: in that respect.  Even gcc -ansi -pedantic doesn't emit a diagnostic
: for this error (it violates the constraint in 6.3.16.1: Simple assignment,
: referred to by 6.5.8: Initialization):

:   char weekday[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };

: (That's an actual example of something a friend who is just learning C
: stumbled over.)

I'm suprised at that; I get 'number of initialisers cannot be greater
than the number of aggregate members' with the compiler I use.  It's
the sort of thing you'd want checked, too.


Will
cwr@crash.cts.com




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
@ 1999-04-15  4:00 ` Noone Really
  1999-04-15  4:00 ` Casper H.S. Dik - Network Security Engineer
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 11+ messages in thread
From: Noone Really @ 1999-04-15  4:00 UTC (permalink / raw)


John Hascall wrote:
>    Is the result of:
> 
>           constant-expression comma-operator constant-expression
> 
>    still a constant-expression or not?  If not, why not (and it

No.

>    seems to me this is the only operator with this property?)

Actually that depends on what exactly you mean. "hello"[0] is not a
constant expression either, even though 'h' and "hello" both are (the
first is integral constant expression and the second is pointer constant
expression).  However, except for the comma operator and operators with
possible side effects, a diagnostic is not required and an
implementation may, at its discretion, accept other forms.

It matters in very few places.  One you have discovered, the other is
exemplified by the fact that (void*)(0,0) is not a null pointer
constant.  The latter again rarely matters because an implementation
gets to choose (but has to document) what (void*)(0,0) becomes, except
in the one context exemplified by the following

int main(void) {
  int a[3];
  *(0? (void*) (0,0) : a) = 0;
  return a[0];
}

is invalid and must get a diagnostic, even though the form with (0,0)
replaced by 0 is perfectly valid C.

> 
> John
> PS, FWIW, gcc accepts both of these and produces the 'expected' results

Old versions of gcc, and current versions if you do not ask it to be an
ANSI C compiler (you need the -pedantic switch).
---




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: comma operator: C compiler bug or bogus usage?
  1999-04-15  4:00   ` John Hascall
  1999-04-15  4:00     ` Kaz Kylheku
@ 1999-04-16  4:00     ` Lawrence Kirby
  1 sibling, 0 replies; 11+ messages in thread
From: Lawrence Kirby @ 1999-04-16  4:00 UTC (permalink / raw)


In article <7f5fvl$ujn$1@news.iastate.edu>
           john@iastate.edu "John Hascall" writes:

...

>     Heh, well any compiler that defaults to trigraphs off gets my cheers!

Such a compiler is doing you no favours, it is just storing up problems
for later on the development/porting process where they caqn do most damage.

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~1999-04-16  4:00 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1999-04-15  4:00 comma operator: C compiler bug or bogus usage? John Hascall
1999-04-15  4:00 ` Noone Really
1999-04-15  4:00 ` Casper H.S. Dik - Network Security Engineer
1999-04-15  4:00 ` Kaz Kylheku
1999-04-15  4:00   ` John Hascall
1999-04-15  4:00     ` Kaz Kylheku
1999-04-16  4:00     ` Lawrence Kirby
1999-04-15  4:00 ` Joerg Czeranski
1999-04-15  4:00   ` Will Rose
1999-04-15  4:00 ` Lawrence Kirby
1999-04-15  4:00 ` Venkat

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox