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

2023-10-27

平台和编译器决定 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.

请使用 signed char 或者 unsigned char,明确指定数据类型,谨慎使用 char

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

平台和编译器决定 char 是 signed char 或者 unsigned char 的相关文章

随机推荐

  • mybatis plus中编写sql语句

    sql 语句是写在对应的xml文件中 首先要解决maven默认不加载xml文件的问题 1 首先要写入相关配置文件 在pom 导入下面内容
  • 网工学习笔记(四):办公网络布线

    办公网络布线 1 综合布线系统主要由哪几个子系统组成 工作间子系统 水平子系统 设备间子系统 垂直间子系统 管理间子系统 建筑群子系统 2 工作区子系统又称为什么 服务区子系统 3 水平子系统连接哪两个子系统 工作间子系统 管理间子系统 4
  • 通讯录进阶——动态通讯录

    通讯录进阶 动态通讯录 大体思路 改编通讯录的结构体 初始化通讯录的修改 增加函数的修改 退出通讯录函数的实现 代码一览图 Contact c main c Contact h 大体思路 今天我们将来实现一下基于我之前博客里的通讯录的进阶
  • Linux 练习十三 (Linux网络编程epoll + 源码练习)

    文章目录 4 EPOLL多路复用 4 1 epoll介绍 4 2 epoll接口的使用 4 3 示例 使用epoll实现即时聊天 4 4 epoll和select的优缺点 使用环境 Ubuntu18 04 使用工具 VMWare works
  • QT的学习2(2020.06.06)

    QT模块 QT基础 模块主要分为 1 QT COre 提供核心的非 GUI 功能 所有模块都需要这个模块 这个模块的类包括了动画框架 定时器 各个容器类 时间日期类 事件 IO JSON 插件机制 智能指针 图形 矩形 路径等 线程 XML
  • 4-数据结构-线性表-顺序表的查找和修改以及总结

    问题 顺序表的查找以及修改 跟数组一样 直接进行遍历查找 以及直接找到数组中某个值 思路 查找 目的时找到对应值的数组下标 输入所需数组 所需查找的值 对顺序表中数组进行遍历 若找到 则返回下标即可 输入你想删除的值 然后实现删除操作 流程
  • 从键盘任意键入一个正整数x,编程计算x的每一位数字相加之和(使用递归方法)。例如输入123,输出结果:6

    这个题对我这个小白来说还挺难的 僵持了好长时间 才想到这个方法来实现 第一步 当然是要从键盘输入一正整数数据啦 第二步 由于考虑到要一直除10和模10 所以我想到的办法是递归 这就要创建一个自己的函数 说起递归这两个方面超级重要 考虑到这个
  • Centos忘记root密码

    我们在维护Centos系统中 一旦忘记root密码 就无法进入系统 这个时候我们又不希望重新安装系统 因为Centos部署着公司运行的正常的业务程序 可以通过以下步骤重新设置root密码 步骤 1 重启系统 然后再五秒之内按下任意键 进入下
  • python退出命令行

    三种方式 1 exit 回车 2 quit 回车 3 control z 回车 注意上面exit和quit后面都有括号
  • 浅析vue3中的声明响应式数据 ref 和 reactive

    在Vue2中响应式数据是通过defineProperty来实现的 而在Vue3响应式数据是通过ES6的Proxy来实现的 Vue3中实现响应式数据的方法是ref和reactive defineProperty只能单一地监听已有属性的修改或者
  • 求取圆形区域内的平均灰度值

    include
  • LaTeX学习:Texlive 2019和TeX studio的安装及使用

    文章目录 1 LaTex介绍 2 Texlive2019的下载和安装 1 下载 2 安装 3 TeXstudio的安装以及简单使用 1 设置中文界面 2 添加行号 3 设置编译器与编码 4 第一个简单程序 4 扩展 1 LaTex介绍 La
  • C51单片机开发程序报错 main.c (11) : error C267 : ‘Nieix‘ : requires ANSI-style prototype

    问题 C51单片机开发程序报错 main c 11 error C267 Nieix requires ANSI style prototype 详细问题 解决方案一 在主函数中调用处修改函数名为函数定义声明处 h文件中 以及函数实现处 c
  • 嵌入式(网络编程扩展)

    一 域名解析 把www XXX XXX COM转换成IP地址 返回值 将域名转化成IP 只适用于IPV4 1 加 include netdb h 2 结构体变量 struct hostent hs NULL 3 4 二 网络属性设置 选项
  • DELL R730安装xenserver 6.2,raid卡驱动问题

    使用dell 730 安装xenserver6 2 提示this host does not appears to have any disk 不能发现硬盘 解决方法 定位原始是xenserver安装包中缺少raid卡驱动 参考 http
  • android o android8.0 startforegroundservice startforegroundservice() did not then call service.star

    解决context startforegroundservice did not then call service startforeground 原因 Android 8 0 系统不允许后台应用创建后台服务 故只能使用Context s
  • 某些科技外企结束在中国市场直接运营,你如何看?

    在新的竞争态势下 向左走 还是向右走 全球存储观察 热点关注 前些天 我发了一篇文章 你如何看LinkedIn 领英职场 将于8月9日起停止中国服务 引发了业内朋友的热议 大家一致认为 科技外企在中国市场的发展何去何从在于其自身定位与全球发
  • 关于Bean的六种作用域

    文章目录 前言 一 singleton 单例作用域 二 prototype 原型作用域 多例作用域 三 request 请求作用域 四 session 会话作用域 五 application 全局作用域 六 websocket HTTP W
  • 对微信小程序的理解

    前言 相信很多人对微信小程序都比较情有独钟 首先大家应该知道小程序的优点 它实现了应用 触手可及 的梦想 用户扫一扫或者搜一下即可打开应用 也体现了 用完即走 的理念 用户不用关心是否安装太多应用的问题 应用将无处不在 随时可用 但又无需安
  • 平台和编译器决定 char 是 signed char 或者 unsigned char

    平台和编译器决定 char 是 signed char 或者 unsigned char The C and C standards allows the character type char to be signed or unsign