HoboBen
HoboBen

Reputation: 3100

How can you make a C compiler assume decimal literals (e.g. 1.23) are float and not double?

In my source code, if I write 1.23 as a literal, e.g. doThis(1.23), gcc assumes it's a double.

Rather than type doThis((float) 1.23), is there a way to use floats for decimal literals/constants unless otherwise specified in an individual source file?

Mega-bonus points, is there a way that works across (nearly) every C compiler?

Upvotes: 1

Views: 468

Answers (4)

Paul Beusterien
Paul Beusterien

Reputation: 29602

Also gcc has the option -fsingle-precision-constant that tells the compiler to treat constants as single precision. See http://gcc.gnu.org/wiki/FloatingPointMath

Upvotes: 2

Peter Miehle
Peter Miehle

Reputation: 6080

try 123.4F for a float constant

Upvotes: 2

Roland Illig
Roland Illig

Reputation: 41686

Yes, the standard way is to write 1.23f. It works with every C compiler, since it is defined in ISO C99 section 6.4.4.2 Floating constants. ISO C90 and K&R have similar definitions.

Upvotes: 7

Bas Bossink
Bas Bossink

Reputation: 9708

try:

float fred = 0.37f;

Upvotes: 5

Related Questions