> 文章列表 > 平台和编译器决定 char 是 signed char 或者 unsigned char

平台和编译器决定 char 是 signed char 或者 unsigned char

平台和编译器决定 char 是 signed char 或者 unsigned char

平台和编译器决定 charsigned char 或者 unsigned char

The C and C++ standards allows the character type char to be signed or unsigned, depending on the platform and compiler. Most systems, including x86 GNU/Linux and Microsoft Windows, use signed char, but those based on PowerPC and ARM processors typically use unsigned char. This can lead to unexpected results when porting programs between platforms which have different defaults for the type of char.
C and C++ 标准允许字符类型 char符号或无符号,具体取决于平台和编译器。大多数系统,包括 x86 GNU/Linux and Microsoft Windows,都使用 signed char,但基于 PowerPC and ARM 处理器的系统通常使用 unsigned char。在 char 类型具有不同默认值的平台之间移植程序时,这可能会导致意外结果。

MacOS X (Darwin) on PowerPC uses signed char, for consistency with other Darwin architectures.

-fsigned-char - Allows the type char in the native libraries to be signed, like signed 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. By default on Android NDK char type is unsigned, but char is treated as signed on x86.

1. Using the GNU Compiler Collection (GCC)

GCC, the GNU Compiler Collection
https://gcc.gnu.org/

GCC 12 Release Series
https://gcc.gnu.org/gcc-12/

12.2 manuals
https://gcc.gnu.org/onlinedocs/12.2.0/

Using the GNU Compiler Collection (GCC)
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/

3 GCC Command Options -> 3.4 Options Controlling C Dialect
https://gcc.gnu.org/onlinedocs/gcc-12.2.0/gcc/C-Dialect-Options.html

1.1 -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.
请注意,这等同于 -fno-unsigned-char,它是 -funsigned-char 的否定形式。同样,选项 -fno-signed-char 等同于 -funsigned-char

1.2 -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.
理想情况下,可移植程序在取决于对象的符号时应始终使用 signed char or unsigned char

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.
char 类型始终是与 signed charunsigned char 都不同的类型,即使它的行为总是类似于这两者之一。

2. Arm C/C++ Compiler Developer and Reference Guide - Version: 23.04

https://developer.arm.com/documentation/101458/2304/

2.1 C/C++ Options

5. Compiler options -> 5.1 Arm C/C++ Compiler Options by Function -> C/C++ Options

Options that affect the way C workloads are compiled.
影响 C workloads 编译方式的选项。

Option Description
-fcommon Place uninitialized global variables in a common block (将未初始化的全局变量放在公共块中)
-fsigned-char Set the type of char to be signed (-fsigned-char) or unsigned (-fno-signed-char [default]).
-std= Language standard to compile for (编译的语言标准)

2.2 -fsigned-char

5. Compiler options -> 5.26 -fsigned-char

Set the type of char to be signed (-fsigned-char) or unsigned (-fno-signed-char [default]).

Default
Default is for the type of char to be unsigned, -fno-signed-char.
默认情况下,char 类型是无符号的。

Syntax

armclang -fsigned-char, -fno-signed-char

3. TI Arm Clang Compiler Tools User’s Guide - 2.1.0.LTS

https://software-dl.ti.com/codegen/docs/tiarmclang/compiler_tools_user_guide/index.html

3.1 Scalar Data Types

2. C/C++ Language Implementation -> 2.1. Data Types -> 2.1.1. Scalar Data Types

The table below lists the size and range of each scalar type as supported in the tiarmclang compiler. Many of the minimum and maximum values for each range are available as standard macros in the C standard header file limits.h.

The storage and alignment of data types is described in Object Representation.

在这里插入图片描述

Notes:
The plain char type has the same representation as either signed char or unsigned char. The -fsigned-char and -funsigned-char options control whether plain char is signed or unsigned. The default is unsigned.

4. ARM Compiler v5.06 for uVision armcc User Guide

https://developer.arm.com/documentation/dui0375/latest/

4.1 --signed_chars, --unsigned_chars

7 Compiler Command-line Options -> 7.148 --signed_chars, --unsigned_chars

Makes the char type signed or unsigned. When char is signed, the macro __FEATURE_SIGNED_CHAR is also defined by the compiler.

Note

  • Care must be taken when mixing translation units that have been compiled with and without this option, and that share interfaces or data structures.
  • The ARM ABI defines char as an unsigned byte, and this is the interpretation used by the C++ libraries.

Default
The default is --unsigned_chars.
默认值为 --unsigned_chars

4.2 Character sets and identifiers in ARM C and C++

10 C and C++ Implementation Details -> 10.1 Character sets and identifiers in ARM C and C++

Data items of type char are unsigned by default.
默认情况下,char 类型的数据项是无符号的。

They can be explicitly declared as signed char or unsigned char:

  • the --signed_chars option makes the char signed
  • the --unsigned_chars option makes the char unsigned.

Note

  • Care must be taken when mixing translation units that have been compiled with and without the --signed_chars and --unsigned_chars options, and that share interfaces or data structures.
  • The ARM ABI defines char as an unsigned byte, and this is the interpretation used by the C++ libraries supplied with the ARM compilation tools.

5. Arm Compiler for Embedded Arm C and C++ Libraries and Floating-Point Support User Guide - Version 6.20

https://developer.arm.com/documentation/100073/0620

5.1 Warning

2. The Arm C and C++ Libraries -> 2.1 Support level definitions -> List of known unsupported features

The Arm ABI defines char as an unsigned byte, and this is the interpretation used by the C libraries supplied with the Arm compilation tools.
Arm ABI 将 char 定义为 unsigned char,这是 Arm 编译工具提供的 C 库使用的解释。

6. Plain char is signed

There is a difference between a plain char and a signed char. By default, a plain char represents an unsigned char value. Using the compiler directive --signed_chars changes the default behavior of plain char to be a signed char.

在这里插入图片描述

References

https://yongqiang.blog.csdn.net/
https://www.linuxtopia.org/online_books/an_introduction_to_gcc/gccintro_71.html
https://expertise.jetruby.com/android-ndk-using-c-c-native-libraries-to-write-android-apps-21550cdd86a