C programming/Typecast

From Teknologisk videncenter
< C programming
Revision as of 16:56, 18 May 2023 by Heth (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Avoid typecasting when possible. If necessary make it clear.

unsigned long responsetime;  // 64 bit type
unsigned int overflows;           // 32 bit type
unsigned short tarvalue;         // 16 bit type
// Next line (long) cast necessary as overflows are only int (Bad programming - avoid casts)
// DO NOT: responsetime=((long) overflows * 0x10000)+e.tarvalue;  // Works but Hard to read  
responsetime=overflows; // Going from smaller to bigger type is OK. int to long
responsetime=(responsetime*0x10000)+e.tarvalue; //short to long