Wednesday 22 August 2007

Writing Portable Code

In 'C' or some other programming languages like C++, we often used to declare a variable like this

int a;

we still assume that the size of the integer is 2 bytes (16 bits). But it is not in all cases. The size of the integer depends on the word size of a corresponding processor.
So, This kind of variable declaration leads to the non portable code across different hardware. So, we should declare the variables only based on WORD size of the processor

We may try the following code

typedef char int8_t; (for 8 bit interger)
typedef unsigned char uint8_t; (for 8 bit unsigned interger)

uint8_t a;

we can make use of stdint.h for other size variables.

No comments: