When you type a number somewhere in your code, it's called a constant. For a number of reasons this constant needs to have a type. Just like variables needs to have a type. Example:
int x; // Obviously x is a variable/object of type: int
1234 // This is an integer constant but what is the type ???
The C standard describes how the type of numeric constants are detemined. For instance the 1234 in the above code will be considered as an interger constant of type int
.
Likewise for floating point constants. There need to be rules for the type of floating point constants. Example:
3.14 // Floating point constant of type ??
And that is exactly what the text in the question tells you:
Floating-point constants are normally represented as double-precision quantities in C
So from this you know that
3.14 // Floating point constant of type double
And here is the quote from the C (draft N1570) standard:
An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has
type float. If suffixed by the letter l or L, it has type long double.
This means:
3.14 // Type: double
3.14F // Type: float
3.14L // Type: long double
So why/when is this important?
At least one example is arithmetic expressions. When doing calculations the type of the operands must be the same. If they differ from the start, there will be conversions so the operands have the same type before the calculation is done. Example:
int x = 42;
double y = 3.14 * x;
So here x
(which is an int
) will be converted to a double because the constant 3.14
is a double. Once x
has been converted the multiplication will be done as double * double
and give a double as result..
Note that it's irrelevant for the multiplication part that the code above saves the result in a double. If you do:
int x = 42;
float y = 3.14 * x;
the same happens for the multiplication, i.e. the multiplication will still be done as double * double
and the result of the multiplication is still a double. The resulting double is then assigned to a float which involves conversion from double to float. Notice however that during the conversion of the result from double to float some precision may be lost so the value of y
in the two examples may differ a little.
To end this post here is some code where it makes a huge difference.
int x = 5;
double y = x / 2.0; // Floating point constant of type double
double z = x / 2; // Integer constant of type int
printf("y=%f z=%f\n", y, z);
Output:
y=2.500000 z=2.000000
Despite both y
and z
being equal to x
divide by two, they end up with different values. This is because the division for calculating y
was done as double / double
while z
was calculated as int / int