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; [..]
ulTime = (dword)(ucS + ucM*60 + ucH*3600);
ulTime = ucS + (dword)ucM*60 + (dword)ucH*3600);
rather than
#define dword unsigned long
typedef unsigned long dword
try this: unsigned char h,m,s; unsigned long int test1; h=7; m=42; s=10; test1 = (unsigned long) ((unsigned long)(h * 3600) + (unsigned long) (m * 60) + s); This works properly on my system.
#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;
(ulong) = (uchar) + (uchar) * (int) + (uchar) * (int);
char * int = int char * long = long int * long = long int * int = int
(ulong) = (uchar) + (uchar) * (int) + (uchar) * (int); /* Is reduced to */ (ulong) = (uchar) + (int) + (int); /* Which is reduced to */ (ulong) = (int) + (int); /* Which is further reduced to */ (ulong) = (int);
ulTime = ucS + ucM * 60UL + ucH * 3600UL;
(ulong) = (uchar) + (uchar) * (ulong) + (uchar) * (ulong); /* Is reduced to */ (ulong) = (uchar) + (ulong) + (ulong); /* Which is reduced to */ (ulong) = (ulong) + (ulong); /* Which is further reduced to */ (ulong) = (ulong);
#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. 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
Hi Jon, thank you for your detailed reply. I will check whether your suggestion will work in all cases or not :-). I hope fervently that C166 will keep to ANSI-C! And thanks to the other repiers too - of course! ...Leo
I will check whether your suggestion will work in all cases or not :-). I hope fervently that C166 will keep to ANSI-C! The C166 compiler is ANSI compliant as tested using the test suites that we have. My suggestion is one of many ways to do things, but it should work with ANY ANSI C compilter. Jon