...

View Full Version : C Code "f" in numbers?



Spookster
04-19-2004, 08:30 PM
I keep seeing the letter "f" being used with numbers in C coding. What is the "f" for? I presume it's either for formatting or precision? Example:

variable = 1.5f

or

variable = 6.f

shmoove
04-19-2004, 09:51 PM
It stands for float. You use it to tell a compiler that a numeric literal (ie, a hard-coded constant) is of type float, and not an int or long or double.

shmoove

Spookster
04-19-2004, 09:57 PM
Oh ok.

So then what would be the difference between:

6.0f and 6.f?

Also I guess in C then the literals do not get automaically converted over to the datatype of the variable?

float number = 6.0; ???

You would have to still designate the float with an f?

float number = 6.0f; ???

shmoove
04-19-2004, 10:43 PM
6.0 could be a double or a float. 6f would be a better reason to use it.
I don't know exactly when it is really useful and when it isn't, and I suspect it strongly depends on the compiler used. If nothing else, it is just there to make sure the person reading the code knows what you're talking about:


float f = 6; // pretty clear that 6 is a float (assuming a smart enough compiler)

// on the other hand
float f;
// many lines of code
f = 6; // now it's not as clear


shmoove

liorean
04-19-2004, 10:50 PM
It's most often used for values that will be used in divisions or modulo, before they are assigned to a variable that has a certain type, to ensure that the resulting value is floatingpoint instead of integer.

Spookster
04-19-2004, 11:17 PM
Ok. Thanks.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum