Re: Suppressing the warning: "variably modified '<variable>' at file scope" generated due to floating-point computation in an integer expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Dec 20, 2024 at 01:46:25PM +0200, beni.falk--- via Gcc-help wrote:
> Hi,
> 
> I would like to write C code such as the following:
> 
> #define X_MIN -7.5
> #define X_MAX 12.7
> #define X_STEP 0.1
>  
> #define NPOINTS ((unsigned) (X_MAX - X_MIN) / X_STEP))
>  
> static int state[NPOINTS];
>  
> However, it generates a compiler warning as follows:
> 
> variably modified 'state' at file scope

When you use a macro, you should always almost parenthesise it, like
#define NPOINTS ((unsigned) ((X_MAX) - (X_MIN)) / (X_STEP)))
Macro text replacement is textual.

And your floating point math is almost certainly not what you want like
this.  Most machines use binary floating point, which gives you ronding
errors here, and conversion from float to integer is truncation, likely
not what you intended.

> The above warning is correct, as far as C language definition is concerned
> (any floating-point constants participating in an integer expression must be
> immediate operands of casts).

Huh?

> I am used to being able to suppress any kind of GCC compiler warning by
> using an appropriate compiler option of pragma. How can the above warning be
> suppressed?

By writing better code?  :-)  Less fragile, etc.


Segher



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux