3
Feb/08
0

Variable Signess : Portability Problem

As you might have already noticed I released my first e-book about these tips & tricks. At the same time I changed the service name, it is not anymore programming tips & tricks, now it is software development tips & tricks. But don’t worry, you are already getting newer information as this trick won’t be in that book. ;)

So, have you ever considered variable signess?

Variable signess means signed or unsigned variable. Let’s use an classic example of char signess.

  • Unsigned char (range : 0 -> 255)
  • Signed char (range : -127 -> 127)

What does this mean? Well it means if you define char variable as 255 and the it is signed it becomes -1. But the real thing why you should care about this is that on different platforms the default char type has different signess!

Default char signess :

  • Windows / Linux : signed char
  • Power PC / ARM : unsigned char

So now if you code do this : char myValue = 255; – in Windows it will be -1, but in Power PC it is 255! Yes, this leads to some serious problems. But there is a solution, typedef your own char type with predefined signess :

typedef unsigned char MyUChar; – and – typedef signed char MySChar; – for example. Now you can do this : MyUChar myValue = 255; – and it will be right in Windows and in Power PC.

Post to Twitter

Filed under: Personal
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.