Floating Point Constants in C

In this lesson we will see how to write floating point constants in C language.

We will learn the syntax for writing in scientific notation and how to force the type of constants.

Floating Point Literal Values in C

When we insert literal constants inside our C programs, just as for integers, we can write the floating point number directly using the dot to separate the integer part from the fractional part.

Therefore, for example, we can write the following perfectly valid code:

double pi = 3.14159;
double e = 2.71828;

The compiler will directly convert the literal value into a floating point number.

The C language offers certain flexibility in writing floating point constants.

For example, if the integer part or the fractional part is equal to zero, we can omit it. The important thing is that, to distinguish a floating point number from an integer, we must insert at least a dot. Furthermore, we cannot omit both parts.

For example, the following numbers are all valid:

double zero = 0.0;
double one = 1.;
double half = .5;

In the first case the number is equal to zero and at least the integer part or the fractional part must be specified. We cannot, in fact, write simply ., but we could write either 0. or .0.

In the second case, instead, the fractional part is omitted, 1., but the dot is still present. This allows the compiler to understand that it is a floating point number. If we had written, in fact, 1 the compiler would have interpreted the number as an integer.

Similarly for the third case, where the integer part is omitted: .5.

Definition

Syntax of Floating Point Constants in C

The syntax for specifying a floating point literal constant in C is the following:

<integer part>.<fractional part>

where:

  • <integer part>: integer part of the floating point number;
  • <fractional part>: fractional part of the number.

The integer part and the fractional part can be omitted if equal to zero, but not both.

The dot is mandatory to distinguish a floating point number from an integer.

Normally, floating point constants are considered as double by the compiler. Therefore, if we write the following code:

float f = 3.14;

what happens is that the value 3.14 is considered as a double and then automatically converted to float. This process, however, could in some cases lead to a loss of precision since a float uses fewer bits than a double. In fact, in these cases compilers show a warning.

To solve this problem, the suffix f or F can be used to force the compiler to consider the constant as a float:

float f = 3.14f;

Similarly, to force a floating point constant to be considered as a long double, the suffix l or L can be used:

long double ld = 3.14l;
Definition

Forcing the Type of Floating Point Constants

To force the compiler to consider a floating point literal constant as a specific type, a suffix can be appended to the constant itself. The suffixes are:

  • f or F for float;
  • l or L for long double.

Scientific Notation in C Language

Writing real numbers with the comma (more precisely with the dot) can be convenient for medium values. The problem arises for very large or very small numbers.

For example, if we want to write the number 0.0000000000123 we could write:

double number = 0.0000000000123;

But, in this way, writing the code is cumbersome since it is not immediately clear at a glance how many zeros are present.

For this reason, the C language allows writing numbers in scientific notation just as is done in mathematics, physics and engineering.

The syntax is very simple. Let's return to the example above. The number 0.0000000000123 in scientific notation is written as:

1.23 \times 10^{-11}

Which is much clearer and more immediate.

To write in scientific notation in C, the letter e or E is used followed by the exponent. For example, the number above is written as:

double number = 1.23e-11;

So the syntax is:

<mantissa>e<exponent>

where <mantissa> is the main part of the number and respects the same rules seen above for the decimal point, and <exponent> is the exponent of the number which, instead, is a signed integer number.

Below, we report some examples of numbers written in scientific notation:

/* 1.23 * 10^-11 */
double number1 = 1.23e-11;

/* -6.02 * 10^23 */
double number2 = -6.02E23;

/* 3 * 10^8 */
double number3 = +3.E8;

/* 0.45 * 10^10 */
double number4 = .45e10;

Even in the case of numbers written in scientific notation we can force the type by appending F or f for float and L or l for long double.

float f = 1.23e-11f;
long double ld = 1.23e-11l;
Definition

Syntax of Floating Point Constants in Scientific Notation

The syntax for specifying a floating point literal constant using scientific notation in C is the following:

<mantissa>e<exponent><type>

where:

  • <mantissa>: main part of the floating point number;
  • e: letter e or E that separates the mantissa from the exponent;
  • <exponent>: exponent of the number;
  • <type>: suffix to force the type of the constant. The suffix can be:

    • f or F for float;
    • l or L for long double;
    • if omitted the constant is considered as double.

In Summary

In this lesson we have seen how to write floating point constants in C inside the code.

We have seen that:

  • floating point constants are written with the dot to separate the integer part from the fractional part;
  • the integer part or the fractional part can be omitted if equal to zero, but not both;
  • to force the type of the constant a suffix can be appended: f or l;
  • constants can be written in scientific notation using the letter e or E.

In the next lesson we will see how to read and write floating point numbers from and to console.