Gcc: force compiler to use unsigned char by defaul

2019-04-06 06:37发布

Since the nature of a char in C++ is compiler-dependent when the unsigned qualifier is not present, is there an argument I could pass on to GCC which would force all chars to be compiled as unsigned?

5条回答
我只想做你的唯一
2楼-- · 2019-04-06 06:53

You can use the option: -funsigned-char.

source

查看更多
放荡不羁爱自由
3楼-- · 2019-04-06 06:59

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.

查看更多
对你真心纯属浪费
4楼-- · 2019-04-06 07:04

You can use -funsigned-char for that, or -fsigned-char for signed.

查看更多
别忘想泡老子
5楼-- · 2019-04-06 07:12

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.

查看更多
Rolldiameter
6楼-- · 2019-04-06 07:17

Not enough reputation to comment @Keith's answer.

The main reason I see to use -funsigned-char or -fsigned-char is when you want the following code

  printf("%d\n",'\x80');

to display either -128 or 128.

The comment about not relying on specific compilation settings is valid but here it is a bit simplifying reality. The main problem is that C leaves some part of the language to the implementation for efficiency reason, and usually you want to tune that implementation to suit best your application. In my opinion a professional developer should always go through all compiler flags and select the best for his needs. If you rely on some specific settings, your unit tests will of course cover that case, or assert it. If you port an application, you will look into the compilation settings of the original port.

查看更多
登录 后发表回答