Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - POSIX incompatibility of constants from <float.h>: (3 Items)
   
POSIX incompatibility of constants from <float.h>  
While trying to compile this code:

//---------------------------------------------------
#include <float.h>

const double test = DBL_MAX;
//---------------------------------------------------

with qcc I get the following error:
float_test.c:3: error: initializer element is not constant
cc: /opt/qnx641/host/linux/x86/usr/lib/gcc/i386-pc-nto-qnx6.4.0/4.3.3/cc1 error 1

while according to standards (SUS: http://www.opengroup.org/onlinepubs/9699919799/basedefs/float.h.html, how about POSIX
?) this "shall be defined as constant expressions". Compiling this code with QCC works as expected.
Re: POSIX incompatibility of constants from <float.h>  
Piotr Trojanek wrote:
> While trying to compile this code:
> 
> //---------------------------------------------------
> #include <float.h>
> 
> const double test = DBL_MAX;
> //---------------------------------------------------
> 
> with qcc I get the following error:
> float_test.c:3: error: initializer element is not constant
> cc: /opt/qnx641/host/linux/x86/usr/lib/gcc/i386-pc-nto-qnx6.4.0/4.3.3/cc1 error 1
> 
> while according to standards (SUS: http://www.opengroup.org/onlinepubs/9699919799/basedefs/float.h.html, how about 
POSIX?) this "shall be defined as constant expressions". Compiling this code with QCC works as expected.

gcc/qcc defaults to C90 + GNU extensions. DBL_MAX and others were not 
required to be a constant expression in C90.

"FLT_RADIX shall be a constant expression suitable for use in #if 
preprocessing directives; all other values need not be constant 
expressions."

The requirement in C99 was added so the float values could be used as 
initializers.  Our headers are conditioned based on __STDC_VERSION__ and 
so if you compile with -Wc,-std=c99 DBL_MAX will be defined as a 
constant expression. e.g. on x86 DBL_MAX will be 0xf.ffffffffffff8p+1020.

Regards,

Ryan Mansfield
Re: POSIX incompatibility of constants from <float.h>  
> gcc/qcc defaults to C90 + GNU extensions. DBL_MAX and others were not 
> required to be a constant expression in C90.
> 
> "FLT_RADIX shall be a constant expression suitable for use in #if 
> preprocessing directives; all other values need not be constant 
> expressions."
> 
> The requirement in C99 was added so the float values could be used as 
> initializers.  Our headers are conditioned based on __STDC_VERSION__ and 
> so if you compile with -Wc,-std=c99 DBL_MAX will be defined as a 
> constant expression. e.g. on x86 DBL_MAX will be 0xf.ffffffffffff8p+1020.

Interesting, what could be the benefit for such a constant of not being a constant expession? Was anybody going to take 
an address of DBL_MAX? :) Or was it better for quality of code being compiled? :)