C casting

This is a discussion on C casting within the C Programming forums, part of the General Programming Boards category; Can anyone tell me the difference between: Code: float answer = 3.0f; Code: float answer = (float)3.0; I understand if ...

  1. #1
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84

    C casting

    Can anyone tell me the difference between:


    Code:
    float answer = 3.0f;
    Code:
    float answer = (float)3.0;
    I understand if you have a variable you can't append an "f" on it, you would have to cast it. Is the second one bad coding or not recommended? Will the compiler do anything different for each one? I always thought the first was sort of shorthand for the second, but I wasn't sure. The second one is called a "cast," but what is the first one called? Thanks!

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I believe that the first one is natively taken as a float, and the 2nd one is taken as a double and then converted to a float. The first one is done at compile-time and the 2nd at runtime, unless the compiler is smart enough to optimize.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by Loctan View Post
    I understand if you have a variable you can't append an "f" on it, you would have to cast it.
    Yes, for numbers, you can append "f" to make it treated like a float. It cannot be done on variables; those must be cast.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    3.0f is a float literal, 3.0 is a double literal.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i think as such there is no difference between 3.0f & (float)3.0 as both the statements do the same thing i.e treat 3.0 as a float.and yes 3.0f is stored as a float and 3.0 as a double.there sizes in memory will also differ.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BEN10 View Post
    i think as such there is no difference between 3.0f & (float)3.0 as both the statements do the same thing i.e treat 3.0 as a float.and yes 3.0f is stored as a float and 3.0 as a double.there sizes in memory will also differ.
    Correct, the same value will end up in answer, but what is in memory that the answer gets loaded from may be different (assuming compiler doesn't optimize it - which is very likely in this case).

    However, if we do something like this:
    Code:
    ... 
       answer = 2.8f;
    
       ...
    
       if (answer == (float)2.8)
          printf("The same\n");
    The result could possibly not be what you expect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21