C programming/Typecast

From Teknologisk videncenter
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