# How to Program in C for the Beginner: Floating-point Numbers and Expressions

## Floating-point numbers – the types float and double

A floating-point number is one which may have a fractional part. A *floating-point constant* can be written in one of two ways:

• the normal way, with an optional sign, and including a decimal point; for example, -3.75, 0.537, 81.792, 47.0;

• using *scientific* notation, with an optional sign, including a decimal point and including an ‘exponent’ part; for example, -0.375E1 meaning “-0.375 multiplied by 10 to the power 1”, that is, -3.75. Similarly, 0.537 can be written as 5.37e-1, that is, “5.37 x 10”. The exponent can be specified using either e or E.

Note that there are several ways to write the same number. For example, the following all represent the same number 27.96:

27.96E00

2.796E1

2.796E+1

2.796E+01

0.2796E+02

279.6E-1

In C, we can declare a floating-point variable using either **float** or **double**. A **float** value is normally stored as a 32-bit floating-point number, giving about 6 or 7 significant digits. A **double** value is stored as a 64-bit floating-point number, giving about 15 significant digits.

A floating-point constant is of type **double** unless it is followed by **f** or **F**, in which case it is of type **float**. Thus 3.75 is of type **double** but 3.75f (or 3.75F) is of type **float**. Most calculations are done using **double** precision. The type **float** is useful if you need to store lots of floating-point numbers and you wish to use as little storage as possible (and can live with 6 or 7 digits of precision).

In this series, we will mostly use **double** when working with floating-point numbers.

## Floating-point expressions

Floating-point expressions can be written using the following operators:

- addition

- subtraction

* multiplication

/ division

These operate as expected; in particular, division is performed in the usual way so that, for example, 19.0/5.0 gives the value 3.8.

Assume **op1** and **op2** are the two operands of an operator (in the expression 2+5: op1=2 and op2=5 are operands; + is the operator), the following shows the type of calculation performed:

- op1 is
**float**and op2 is**float**, type of caculation is**float** - op1 is
**double**and op2 is**float**, type of caculation is**double** - op1 is
**float**and op2 is**double**, type of caculation is**double** - op1 is
**double**and op2 is**double**, type of caculation is**double**

Thus **float** is performed only if both operands are **float**; otherwise **double** is performed.

## References

C Programming – A Beginner’s Course

Related programming references

## This post is part of the series: C Programming for Beginners

A straightforward introduction to Programming in C for people with no previous programing experience.

- C Programming For Beginners - Part 1
- C Programming For Beginners - Part 2
- C Programming for Beginners – Part 3
- C Programming for Beginners – Part 4
- C Programming For Beginners - Part 5
- C Programming For Beginners - Part 6
- C Programming For Beginners - Data Types
- C Programming For Beginners - Part 8
- C Programming For Beginners - Part 9
- C Programming For Beginners - Part 10
- C Programming For Beginners - Part 11
- C Programming For Beginners - Part 12
- C Programming For Beginners - Part 13
- C Programming For Beginners - Part 14
- C Programming For Beginners - Integer Data Types
- C Programming for Beginners - Part 16
- C Programming For Beginners - Integer Expressions, Operators and Precedence
- C Programming For Beginners - Part 18
- C Programming For Beginners - Printing Double and Float
- C Programming For Beginners - Mixing double, float and int