User: Password:
Subscribe / Log in / New account

C89 includes blocks you know...

C89 includes blocks you know...

Posted Jun 12, 2008 5:17 UTC (Thu) by eru (subscriber, #2753)
In reply to: C89 includes blocks you know... by nix
Parent article: Implications of pure and constant functions

Really? Everything I've found, including my reading of the text of C89, seems to say that initializers of automatic variables must be constant (and I use a number of compilers on a daily basis that reject non-constant initializers in C89 mode).

Well, they are broken compilers. Complain to the vendor.

A cite from C89 that supports your interpretation would be nice.

From my photocopy the original X3.159-1989:
3.5.7 Initialization
[syntax omitted]
All the expressions in an initializer for an object that has static storage duration or in an initializer list for an object that has aggregate or union type shall be constant expressions.

Since the standard does not specify such constantness constraint for all objects with automatic storage duration, they can be runtime values.

The Rationale section for 3.5.7 makes this even more clear: According to it, the committee even considered allowing automatic aggregate initializers to consist of series of or arbitrary runtime expressions, but did not go that far in the end. The rationale also mentions that a function call that returns a structure is permitted as an initializer for an automatic variable with structure type. I have used some old compilers that had problems with this kind of structure initialization, but they still allowed automatic scalars to be initialized with runtime values. I think even K&R C allowed this.

(Log in to post comments)

C89 includes blocks you know...

Posted Jun 12, 2008 10:54 UTC (Thu) by nix (subscriber, #2304) [Link]

OK, broken compilers it is. (It's surprising that a language as apparently simple as C can
still trip one up with unexpected corners like this after so long.)

Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds