Difference between revisions of "C programming/Typecast"

From Teknologisk videncenter
Jump to: navigation, search
(Created page with "Avoid typecasting when possible. If necessary make it clear. <source lang=c> unsigned long responsetime; // 64 bit type unsigned int overflows; // 32 bit type unsig...")
(No difference)

Revision as of 16:56, 18 May 2023

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 longl
responsetime=(responsetime*0x10000)+e.tarvalue; //short to longl