Next Previous Contents

6. Use chars instead of ints if possible

While in arithmetic operations, chars are immidiately promoted to ints, they are passed as chars in parameter lists and are accessed as chars in variables. The code generated is usually not much smaller, but it is faster, since accessing chars is faster. For several operations, the generated code may be better if intermediate results that are known not to be larger than 8 bit are casted to chars.

When doing

        unsigned char a;
        ...
        if ((a & 0x0F) == 0)

the result of the & operator is an int because of the int promotion rules of the language. So the compare is also done with 16 bits. When using

        unsigned char a;
        ...
        if ((unsigned char)(a & 0x0F) == 0)

the generated code is much shorter, since the operation is done with 8 bits instead of 16.


Next Previous Contents