[CMake] Question about c99

Tanner Lovelace clubjuggler at gmail.com
Sat Apr 1 18:43:22 EST 2006


On 4/1/06, William A. Hoffman <billlist at nycap.rr.com> wrote:

> Create a try_compile that tests the above featues and sets some
> value like C_COMPILER_IS_C99, then do something like this:
>
> #ifndef C_COMPILER_IS_C99
> #define for if(false) {} else for
> #define bool int
> #endif

1. Pre-C99 doesn't have a native false variable, afaik.
2. Much easier to do  #define for if (1) for

In case someone don't realize why the define is needed,
previously, variables declared in a for statement were
held to have the same scope of the for statement
(that is, accessible outside the for statement).

This would make for code like this:

for (int i = 0; i < 10; ++i) { printf("%d\n",i); }

/* i is already declared so int i here is an error */
for (i = 0; i < 10; ++i) { printf("%d\n",i); }

C99, however, changed the scope of variables
declared in the for statement to only have scope
inside the for statement making it so the second
i above would need to be redeclared.

Since valid code for each compiler is an error
in the other compiler, the redefinition of
for lets you write what is valid C99 code
in older C compilers because for statements
are wrapped inside an if statement and therefore
variables defined in the for have scope only
inside the if statement.  And, since most
optimizing compilers will optimize out the
if anyway, there isn't any runtime penalty.

So, to sum up.  You don't really need C99
to do "for(int i ...)" as long as you use that
kind of define listed above.

Cheers,
Tanner

--
Tanner Lovelace
clubjuggler at gmail dot com
http://wtl.wayfarer.org/
(fieldless) In fess two roundels in pale, a billet fesswise and an
increscent, all sable.


More information about the CMake mailing list