char & UBYTE

This forum is for general developer support questions.
AmiDog
Posts: 5
Joined: Sat Jun 18, 2011 8:09 am

Re: char & UBYTE

Post by AmiDog »

salass00 wrote:UBYTE is always unsigned while char without signed or unsigned keyword can be either signed or unsigned depending on the compiler, the target system and what compiler options are used (f.e. gcc allows to force either signed or unsigned with -fsigned-char and -funsigned-char options).
There are three distinct types of a char: "unsigned char", "signed char" and "char" and GCC will give you a warning in case you try to mix and match them. So if you need an 8-bit value in the range [0,255], use "unsigned char", if you need an 8-bit value in the range [-128,127] use "signed char" and if you need to store a char (or an array of chars, aka string), use the type "char", "char *" or "const char *" (for string constants).
User avatar
salass00
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 530
Joined: Sat Jun 18, 2011 3:12 pm
Location: Finland
Contact:

Re: char & UBYTE

Post by salass00 »

AmiDog wrote:There are three distinct types of a char: "unsigned char", "signed char" and "char" and GCC will give you a warning in case you try to mix and match them. So if you need an 8-bit value in the range [0,255], use "unsigned char", if you need an 8-bit value in the range [-128,127] use "signed char" and if you need to store a char (or an array of chars, aka string), use the type "char", "char *" or "const char *" (for string constants).
I'm aware of that, but some broken code (like original libiffanim) are coded under the assumption that char has a certain signedness (signed in libiffanim case). In this case forcing a certain type for char won't necessarily fix any warnings but at least it will fix the code.

In libiffanim AmigaOS4.x port I fixed it by changing the type of one "char" variable to "signed char" but some other code may require much more code changes in which case simply forcing it by -fsigned-char or -funsigned-char might be more convenient.
Post Reply