#51 – Float Literals Must Use f Suffix
August 7, 2010 Leave a comment
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