#51 – Float Literals Must Use f Suffix

When specifying literals of type double, you don’t need a suffix.  All floating point literals are by default assumed to be of type double.

double d1 = 4.2;    // This works

But when specifying float literals, you need an explicit cast, or the f suffix.

 // 4.2 literal is inferred as double
 float f = 4.2;   // Compilation error: Literal of type double cannot be implicitly converted to type 'float'

 // Two ways to fix this
 float f2 = (float)4.2;      // double explicitly cast to float
 float f3 = 4.2f;            // 'f' indicates that literal is float
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: