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


Is it necessary to add f to a float value within the parameters of a if ?


if (a == 2.5f && a > 2.0f) {}
asked by anonymous 28.08.2018 / 17:41

1 answer


Nothing to do with if , it's a matter of choosing literal you want to use .

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

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

In both there will be no accuracy, such as already shown in your previous question . If that's what you want, you can not use any of them. You need to create your own mechanism, use another form, or a library that treats this correctly.

28.08.2018 / 18:48