Advertisement
Tech

C Programming: Mixing float, double and int in an expression

In this article, we explain what happens when we attempt to assign a float value to a double variable and vice versa. We also discuss how expressions containing different numeric types (int and double, say) are evaluated in C.

By Noel Kalicharan
Desk Tech
Reading time 3 min read
Word count 588
Linux Computing Linux commands
C Programming: Mixing float, double and int in an expression
Advertisement
Quick Take

In this article, we explain what happens when we attempt to assign a float value to a double variable and vice versa. We also discuss how expressions containing different numeric types (int and double, say) are evaluated in C.

On this page

Assignment between double and float

As expected, you can store a float value in a float variable and a double value in a double variable. Since float is smaller than double, C allows you to store a float value in a double variable without any problems. However, if you assign a double to a float, some precision may be lost. Consider the following:

double d = 987.654321;

Advertisement

float x = d;

printf("%f \n", x);

Advertisement

Since a float variable allows at most 7 digits of precision, we should expect that the value of d may not be assigned precisely to x. Indeed, when run using one compiler, the value 987.654297 was printed for x. When d was changed to 987654321.12345, the value printed was 987654336.000000. In both cases, about 6 or 7 digits of precision were retained.

As an exercise, see what values would be printed using your compiler.

Advertisement

Expressions with integer and floating-point values

It is quite common to use expressions involving both integer and floating-point values, for example,

a / 3 where a is float

Advertisement

n * 0.25 where n is int

In C, the rule for such expressions is:

Advertisement

If either operand of an arithmetic operator is floating-point, the calculation is done in floating-point arithmetic. The calculation is done in double unless both operands are float, in which case the calculation is done in float.

In the first example above, the integer 3 is converted to float and the calculation is done in float. In the second example, n is converted to double (since 0.25 is double) and the calculation is done in double.

Advertisement

How do we get the exact value of an integer division, 19/5, say? We can force a double precision calculation by writing one or both constants as double, thus: 19/5.0, 19.0/5 or 19.0/5.0. We can also use a cast, as in

(double) 19 / 5

Advertisement

A cast allows us to force the conversion of one type to another. Here, the int 19 is cast to double, forcing 5 to be converted to double and a double precision division is performed.

However, we must be careful with a construct like

Advertisement

(double) (19 / 5)

since it may not do what we think. This does NOT do a floating-point division. Since both constants are integer, the expression inside the brackets is evaluated as an integer division (which discards the remainder), giving 3; this value is converted to double, giving 3.0.

Advertisement

References

C Programming – A Beginner’s Course

Related programming references

Advertisement

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.

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

More from Tech

Filed under
Linux Computing
More topics
Linux commands
Advertisement