Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reason for ISO ; warning

Reply
Thread Tools

Reason for ISO ; warning

 
 
pete
Guest
Posts: n/a
 
      12-08-2005
Simon Biber wrote:
>
> Wolfgang Riedel wrote:
> > so this should be valid
> > (and compiles with -Wall -pedantic -std=c99):
> >
> > #include <string.h>
> >
> > size_t blah = strlen("abc");
> >
> > /*...*/

>
> It's not valid, and gcc produces the diagnostic message "initializer
> element is not constant"
> when compiled with exactly the options you give.


C99
6.7.8 Initialization

[#4] All the expressions in an initializer for an object
that has static storage duration shall be constant
expressions or string literals.

6.6 Constant expressions

[#7] More latitude is permitted for constant expressions in
initializers. Such a constant expression shall be, or
evaluate to, one of the following:
-- an arithmetic constant expression,
-- a null pointer constant,
-- an address constant, or
-- an address constant for an object type plus or minus an
integer constant expression.

[#8] An arithmetic constant expression shall have arithmetic
type and shall only have operands that are integer
constants, floating constants, enumeration constants,
character constants, and sizeof expressions.

--
pete
 
Reply With Quote
 
 
 
 
Wolfgang Riedel
Guest
Posts: n/a
 
      12-08-2005
Simon Biber wrote:
>
> Wolfgang Riedel wrote:
> > so this should be valid (and compiles with -Wall -pedantic -std=c99):
> >
> > #include <string.h>
> >
> > size_t blah = strlen("abc");
> >
> > /*...*/

>
> It's not valid, and gcc produces the diagnostic message "initializer
> element is not constant" when compiled with exactly the options you give.
>
> --
> Simon.


You snipped the rationale, I quoted.
I think it's correct (evaluates to arithmetic constant exrpssion).

Btw. my gcc doesn't warn (maybe not the latest & greatest):


gcc --version

gcc (GCC) 3.2.2
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

cat z2.c && gcc -Wall -pedantic -std=c99 -o z2 z2.c && echo $? && ./z2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int bah = strlen("abc");
int
main(void)
{
printf("%d\n", bah);
exit(0);
}
0
3
 
Reply With Quote
 
 
 
 
Skarmander
Guest
Posts: n/a
 
      12-08-2005
Wolfgang Riedel wrote:
> Simon Biber wrote:
>> Wolfgang Riedel wrote:
>>> so this should be valid (and compiles with -Wall -pedantic -std=c99):
>>>
>>> #include <string.h>
>>>
>>> size_t blah = strlen("abc");
>>>
>>> /*...*/

>> It's not valid, and gcc produces the diagnostic message "initializer
>> element is not constant" when compiled with exactly the options you give.
>>
>> --
>> Simon.

>
> You snipped the rationale, I quoted.
> I think it's correct (evaluates to arithmetic constant exrpssion).
>

No. The relevant definition is

"An arithmetic constant expression shall have arithmetic type and shall only
have operands that are integer constants, floating constants, enumeration
constants, character constants, and sizeof expressions. Cast operators in an
arithmetic constant expression shall only convert arithmetic types to
arithmetic types, except as part of an operand to a sizeof operator whose
result is an integer constant."

'strlen("abc")' doesn't qualify. gcc is free to evaluate 'strlen("abc")' as
a constant expression, but this does not make it an arithmetic constant
expression. Of course, the initializer to "blah" need only be a constant
expression. It doesn't have to be an arithmetic constant expression
specifically.

> Btw. my gcc doesn't warn (maybe not the latest & greatest):
>

<snip>
Not a bug. Later versions of gcc do warn because standards-conforming
compilers are not required to accept the program, but gcc does not violate
the standard by not issuing a diagnostic -- implementations are expressly
allowed to accept other forms of expressions as constant than the ones
described by the standard.

If you expect 'strlen("abc")' to be a constant expression, you're relying on
an extension. It's not portable code.

Note that gcc does *not* allow expressions that cannot be evaluated at
compile time as initializers, and rightly so. This will not compile:

size_t foo();
size_t blah = foo();

S.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reason for ISO ; warning pemo C Programming 0 12-07-2005 02:58 PM



Advertisments