What is the need to use the suffix "f" in a numerical constant in C?

Asked

Viewed 162 times

8

It is necessary to add f at a value float within the parameters of a if?

Example:

if (a == 2.5f && a > 2.0f) {}
  • 1

    Welcome Eduardo. Although it is not mandatory to insert 'f', it is recommended, as the compiler will interpret the number as float.

  • 1

    In accordance with Floating point constants C, without the suffix the value will be treated as double. There’s a question in Soen about that too: Suffix of "f" on float value?

1 answer

8


It has nothing to do with if, is a matter of choosing the literal who wants to use.

If you want the number to be one float then you should use the f always. If not using and have decimal point it will be by default a double. This type has 64 bits and the float has 32 bits. So with the suffix f or F it will take up less memory space, and of course, will have less precision.

If the number is integer, that is, it does not have a point in the literal, then it will not be double, much less float, unless you use a f.

In both there will be no accuracy, as already shown in previous question yours. If that’s what you want, you can’t use any of them. You need to create a mechanism of your own, use another form or a library that handles it correctly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.