Thread: Casting

  1. #1
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82

    Casting

    Ok, so someone please explain the following to me.

    Code:
    #include <stdint.h>
    
    int64_t a = 0;
    uint32_t b = 1;
    
    int32_t main(void)
    {
      return (signed)a;
    }
    Does this code truncate down to a 32-bit variable because that is the return type? Or because when I say "signed" and do not specify a type, it assumes int?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    signed defaults to int, yes. Whether that is int32 or int64, only your compiler knows (and you, if you bother to read the header or print out sizeof information, or find out in some other way). If signed does not match int32_t, then it will return the least significant 32 bits of the answer (or pad int out to 32 bits, whichever is necessary).

  3. #3
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Ok, but is the truncation because of the return type or because of the lack of specifying what type is signed?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Could be either. If just plain int is int32, then truncation will happen at the cast. If just plain int is int64, then truncation will happen at the return.

  5. #5
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    Ok, so what happens here?

    Code:
    int64_t x = (unsigned)-1;
    On a 32-bit system? x contains 01777777777777777777777 or 037777777777?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Here the cast would happen first, so (unsigned)-1 is 0xffffffff; this fits in 64 bits just fine, so nothing more happens.

  7. #7
    Banned
    Join Date
    Dec 2008
    Location
    Maputo, Mozambique
    Posts
    82
    ok so to get 01777777777777777777777 I need 2 do uint64_t x = (uint64_t)-1?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I'm not sure why you think the cast is necessary (other than to shut the compiler up). And for that matter, if your goal is that number, then just do it:
    Code:
    uint64_t x = 01777777777777777777777LL;

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    signed is short for signed int, which is the same as int.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Ugh octal constants - yuck!
    Unless you're using some kind of bit flags scheme where the groupings of 3 bits are somehow significant - stick with hexadecimal!

    Btw, a nicer way to get an unsigned constant with all bits set is ~0U, or ~0ULL etc.
    It even works for non two's complement systems!
    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"

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