平台和编译器决定 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 的相关文章

  • 在 HKCR 中创建新密钥有效,但不起作用

    我有以下代码 它返回 成功 但使用两种不同的工具使用搜索字符串 3BDAAC43 E734 11D5 93AF 00105A990292 搜索注册表不会产生任何结果 RegistryKey RK Registry ClassesRoot C
  • 如何在类文件中使用 Url.Action() ?

    如何在 MVC 项目的类文件中使用 Url Action Like namespace 3harf public class myFunction public static void CheckUserAdminPanelPermissi
  • 按扩展名过滤搜索文件返回太多结果

    我正在开发一个 C 控制台应用程序 它必须管理 Windows 操作系统上的文件 我需要获取具有特定扩展名的文件名 列表 我找到了很多解决方案 最建议的是以下一种 HANDLE hFind WIN32 FIND DATA data hFin
  • 未找到 Boost 库,但编译正常

    我正在尝试在 C 中使用 boost 的文件系统 使用时看起来编译没问题 c c Analyse c o Analyse o g W Wall L usr local lib lboost filesystem lboost system
  • 当事件button.click发生时,如何获取按钮名称/标签?

    我以编程方式制作按钮并将它们添加到堆栈面板中 以便每次用户导航到页面时按钮都会发生变化 我正在尝试做这样的事情 当我单击创建的按钮时 它将获取按钮的标签并转到正确的页面 但是 我无法使用 RoutedEventHandler 访问按钮元素
  • 处理右值时的 insert 与 emplace

    std string myString std unordered set
  • 强制初始化模板类的静态数据成员

    关于模板类的静态数据成员未初始化存在一些问题 不幸的是 这些都没有能够帮助我解决我的具体问题的答案 我有一个模板类 它有一个静态数据成员 必须为特定类型显式实例化 即必须专门化 如果不是这种情况 使用不同的模板函数应该会导致链接器错误 这是
  • RestSharp获取序列化输出

    我正在寻找一种方法来访问 AddBody 调用的序列化结果 我正在使用内置的 RestSharp 序列化器 例子 class Foo public string FooField void SendRecord var f new Foo
  • 不同 C++ 文件中的相同类名

    如果两个 C 文件具有相同名称的类的不同定义 那么当它们被编译和链接时 即使没有警告也会抛出一些东西 例如 a cc class Student public std string foo return A void foo a Stude
  • 获取没有显式特征的整数模板参数的有符号/无符号变体

    我希望定义一个模板类 其模板参数始终是整数类型 该类将包含两个成员 其中之一是类型T 另一个作为类型的无符号变体T 即如果T int then T Unsigned unsigned int 我的第一直觉是这样做 template
  • C++中判断unicode字符是全角还是半角

    我正在编写一个终端 控制台 应用程序 该应用程序应该包装任意 unicode 文本 终端通常使用等宽 固定宽度 字体 因此要换行文本 只需计算字符数并观察单词是否适合一行并采取相应的操作 问题是 Unicode 表中的全角字符在终端中占用了
  • 在 C# 中检查 PowerShell 执行策略的最佳方法是什么?

    当你跑步时Get ExecutionPolicy在 PowerShell 中 它得到有效的执行政策 https learn microsoft com en us powershell module microsoft powershell
  • 如何递归取消引用指针(C++03)?

    我正在尝试在 C 中递归地取消引用指针 如果传递一个对象 那就是not一个指针 这包括智能指针 我只想返回对象本身 如果可能的话通过引用返回 我有这个代码 template
  • 从 C# 使用 Odbc 调用 Oracle 包函数

    我在 Oracle 包中定义了一个函数 CREATE OR REPLACE PACKAGE BODY TESTUSER TESTPKG as FUNCTION testfunc n IN NUMBER RETURN NUMBER as be
  • 在 C 中使用枚举而不是 #defines 作为编译时常量是否合理?

    在 C 工作了一段时间后 我将回到 C 开发领域 我已经意识到 在不必要的时候应该避免使用宏 以便让编译器在编译时为您做更多的工作 因此 对于常量值 在 C 中我将使用静态 const 变量或 C 11 枚举类来实现良好的作用域 在 C 中
  • C++ - 多维数组

    处理多维数组时 是否可以为数组分配两种不同的变量类型 例如你有数组int example i j 有可能吗i and j是两种完全不同的变量类型 例如 int 和 string 听起来您正在寻找 std vector
  • 了解 Lambda 表达式和委托 [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我已经尝试解决这个问题很长一段时间了 阅读在线博客和文章 但到目前为止还没有成功 什么是代表 什么是 Lambda 表达式 两者的优点
  • 在 Win32 控制台应用程序中设置光标位置

    如何在 Win32 控制台应用程序中设置光标位置 最好 我想避免制作句柄并使用 Windows 控制台功能 我花了整个早上沿着那条黑暗的小巷跑 它产生的问题比它解决的问题还要多 我似乎记得当我在大学时使用 stdio 做这件事相对简单 但我
  • 为什么空循环使用如此多的处理器时间?

    如果我的代码中有一个空的 while 循环 例如 while true 它将把处理器的使用率提高到大约 25 但是 如果我执行以下操作 while true Sleep 1 它只会使用大约1 那么这是为什么呢 更新 感谢所有精彩的回复 但我
  • 当用户更改 Windows 中的语言键盘布局时如何通知?

    I want to show a message to user when the user changes the language keyboard layout of Windows for example from EN to FR

随机推荐

  • 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