Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts

    C Code "f" in numbers?

    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
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #2
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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; ???
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,273
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Ok. Thanks.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •