Gcc: force compiler to use unsigned char by default

The flag you are looking for is -funsigned-char.

From the documentation:

-funsigned-char

Let the type char be unsigned, like unsigned char.

Each kind of machine has a default for what char should be. It is either like unsigned char by default or like signed char by default.

Ideally, a portable program should always use signed char or unsigned char when it depends on the signedness of an object. But many programs have been written to use plain char and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default.

The type char is always a distinct type from each of signed char or unsigned char, even though its behavior is always just like one of those two.


-fsigned-char

Let the type char be signed, like signed char.

Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.

This only impacts char; types like wchar_t are unaffected.


As the other answers say, gcc's -funsigned-char option forces plain char to be unsigned.

But that may not be the best solution to your problem. You want unsigned characters, but by using a compiler-specific option you're encoding that information in the build command (the Makefile, build script, or just the command you type to compile your code). If the semantics of your program depend on having unsigned characters, it's better to record that information in your source code. It's clearer, and it's reduces the chance that someone will build your program incorrectly.

If you want unsigned characters, using unsigned char. If you want signed characters, use signed char. If you just want characters, and you're sure your program's behavior doesn't depend on whether they're signed or unsigned (say, if all stored values are in the range 0..127), use char.