Like Tree1Likes
  • 1 Post By grumpy

difference between a cast and something

This is a discussion on difference between a cast and something within the C Programming forums, part of the General Programming Boards category; Heya! What is the difference between a cast: Code: (long) 10000000 And a something: Code: 10000000L Done! Thanks in advanced ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Chiang Mai, Thailand.
    Posts
    9

    difference between a cast and something

    Heya!
    What is the difference between a cast:
    Code:
    (long) 10000000
    And a something:
    Code:
    10000000L
    Done! Thanks in advanced ขอบคุณล่วงหน้านะครับ

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,634
    10000000 is a literal value. Its type is the smallest of int, long int, or long long int supported by your compiler. The (long) type conversion converts that value from whatever its type is to long. (Note that the C standard did not require support of long long types before 1999).

    10000000L is a literal value with type long. No type conversion is required. The 'L' suffix tells the compiler the value is of type long.


    There is not much difference in your example, practically, since the maximum value of a long is guaranteed to exceed 10000000.

    You might want to ask how the two forms would be different if the value you supplied exceeds what your compiler's long type can represent (e.g. 3000000000 - a value that is not guaranteed to be representable using a long type - rather than 10000000). The first form potentially compiles but then gives undefined behaviour. The second form may not compile with every compiler (the actual range of values a long can support is implementation defined i.e. compiler dependent) but is guaranteed to work if it compiles.
    nonpuz likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The first one would fail to compile if the compiler used 16-bit ints.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    Chiang Mai, Thailand.
    Posts
    9
    Ok thanks all.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Chiang Mai, Thailand.
    Posts
    9
    Dis thread iz now solved! C:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using the (int) cast
    By minyoungan in forum C Programming
    Replies: 2
    Last Post: 09-24-2010, 11:01 AM
  2. Help with Cast
    By afflictedd2 in forum Linux Programming
    Replies: 9
    Last Post: 09-08-2008, 04:32 AM
  3. reinterpret_cast, C-style cast or function-style cast
    By blight2c in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2002, 11:07 PM
  4. Is this a cast?
    By subnet_rx in forum C++ Programming
    Replies: 1
    Last Post: 12-29-2001, 06:00 AM
  5. Cast
    By Matrix01 in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 02:58 AM

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