This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

cast?

Does anybody know, how one have to cast an operation correctly? And is it necessary to tell the compiler that a constant is unsined long

#define dword unsigned long
unsigned char ucH;    //hours
unsigned char ucM;    //minutes
unsigned char ucS;    //seconds
dword ulTime;         //Time in s

GetTime(...);
ulTime = ucS + ucM*60 + ucH*3600;
[..]
this won't work! How do I have to cast the calculation?

ulTime = (dword)(ucS + ucM*60 + ucH*3600);
or
ulTime = ucS + (dword)ucM*60 + (dword)ucH*3600);
or ???

I have tried to find any written description (K&R, C166-Reference) but I could't find anything about this theme!

Do I have to use 3600 or 3600ul?

Thank you
...Leo

Parents
  • #define dword unsigned long
    unsigned char ucH;    //hours
    unsigned char ucM;    //minutes
    unsigned char ucS;    //seconds
    dword ulTime;         //Time in s
    
    ulTime = ucS + ucM * 60UL + ucH * 3600UL;
    

    ucS and ucM are unsigned char, so they each have a maximum value of 255. 255 + 60 * 255 is 15555, well within the range of an int, so the constant sixty doesn't have to be an unsigned long.

    255 * 3600 is 918000, way beyond what a 16-bit int can hold. Even if we assume ucH will have a maximum value of 23, 23 * 3600 is 82800, again too big for an int, so the 3600 does have to be 3600UL.

    -- Gary Culp

Reply
  • #define dword unsigned long
    unsigned char ucH;    //hours
    unsigned char ucM;    //minutes
    unsigned char ucS;    //seconds
    dword ulTime;         //Time in s
    
    ulTime = ucS + ucM * 60UL + ucH * 3600UL;
    

    ucS and ucM are unsigned char, so they each have a maximum value of 255. 255 + 60 * 255 is 15555, well within the range of an int, so the constant sixty doesn't have to be an unsigned long.

    255 * 3600 is 918000, way beyond what a 16-bit int can hold. Even if we assume ucH will have a maximum value of 23, 23 * 3600 is 82800, again too big for an int, so the 3600 does have to be 3600UL.

    -- Gary Culp

Children
  • ucS and ucM are unsigned char, so they each have a maximum value of 255. 255 + 60 * 255 is 15555, well within the range of an int, so the constant sixty doesn't have to be an unsigned long.

    Ahhh. Yes. You are right about that. I noticed that the multiplication with 3600 would definitely be out of range and assumed that the multiplication with 60 would be as well (assuming that NOINTPROMOTE is used.)

    Jon