I have a question concerning floating constants in C.
In Java, the default type of floating point constants in double, so the following will causes a compilation error in java:
float f = 100.0; // we either need to uses type case operator or put f at the end of the number constant.
This is because the default floating-point constants are of type double and casting from double to float without type cast operator is an error, so we need either add a type case operator or put f at the end of the number.
So, Why in C this doesn’t produce an error, Is it because the default floating-point constants are of type float, or because the compiler do an implicit down-cast conversion (that doesn’t requires type case operator in C)????
In C, floating point constants have type
doubleby default, but adoublecan be implicitly converted to afloat(note that you do have to be careful: if the value of the constant is outside the range representable by afloat, the result of such a conversion is undefined).If you want a floating point constant of type
float, you can append the suffixfto the end of the constant.