Reason for ISO ; warning

P

pete

Simon said:
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.
 
W

Wolfgang Riedel

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

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
 
S

Skarmander

Wolfgang said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,329
Latest member
InezZ76898

Latest Threads

Top