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

news2024/10/6 18:28:05

平台和编译器决定 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 编译方式的选项。

OptionDescription
-fcommonPlace uninitialized global variables in a common block (将未初始化的全局变量放在公共块中)
-fsigned-charSet 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/418197.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

NetCore3.1或Net6.0项目升级到Net7.0

其实与我之前发布的步骤基本一致,升级到net6.0之后,在升级net7.0基本没有可修改的代码,只是升级一些nuget包而已,NetCore3.1升级到Net6.0,可参考此文章:NetCore3.1项目升级到Net6.0_csdn_aspnet的博客-CSDN…

Rust China Conf 2023 筹备启动:议题征集开始

大会介绍Rust China Conf 2023 由 Rust 中文社区发起主办、知名企业和开源组织联合协办,是年度国内规模最大并唯一的 Rust 线下大型会议,深受 Rust 中文社区开发者与相关企业的喜爱与推崇。本次大会为线下会议,将于6月17日-18日在上海举办&am…

Java虚拟机总结

前言 Java是目前用户最多、使用范围最广的软件开发技术之一。Java的技术体系主要由支撑Java程序运行的虚拟机、提供各开发领域接口支持的Java APl、,Java编程语言及许多第三方Java框架(如Spring、Struts等)构成。在国内,有关Java APl、Java语…

【蓝桥杯嵌入式】第十四届蓝桥杯嵌入式省赛[第一场]程序设计题以及详细题解

文章目录原题展示原题分析原题题解LED相关LCD相关按键相关ADC相关定时器相关PWM输入捕获小结文章福利原题展示 原题分析 今年的第一场比赛绝对np,官方将串口直接省掉了,将其替换成很多小功能,如:切换计时、频率均匀变化、锁机制等等&#xff…

Angular 全屏后选择器 (nz-select) 下拉选项框失效【开发笔记】

问题:Angular 全屏后选择器 (nz-select) 下拉选择无法使用 如图: 相应解决方法的文章:https://medium.com/shahar.kazaz/adding-fullscreen-support-to-ng-zorro-a38140da676 三种解决方法: ① FullscreenOverlyContainer&#…

SAP中用CS20批量修改BOM应用问题处理实例

在应用中可能会遇到这样的情况,用户通过某个工艺或技术上的改进,节约了某个原料的用量,而这个原料可能应用在一批成品上。如果成品数量太大,就需要做批量的变更了。 CS20这个事务应该就是用于做BOM批量处理的,笔者之前…

C++基础回顾(上)

C基础回顾(上) 目录C基础回顾(上)前言关键字和标识符运算符数据类型函数类前言 C之前学过一点,但是很长时间都没用过,翻出了书从头看了一遍,简短地做了笔记,以便自己之后查看和学习…

5、存储引擎

1、查看存储引擎 查看mysql提供什么存储引擎: show engines;2、设置系统默认的存储引擎 查看默认的存储引擎: show variables like %storage_engine%; #或 SELECT default_storage_engine;修改默认的存储引擎 如果在创建表的语句中没有显式指定表的存…

教你精通Java语法之第十二章、递归

目录 一、递归 1.1递归的概念 1.1.1定义 1.1.2原理 1.1.3思路 1.2单路递归 1.2.1阶乘 1.2.2正向输出数字 1.2.3反向输出字符串 1.3多路递归 1.3.1斐波那契数列 1.3.2兔子问题 1.3.3青蛙爬楼梯 1.4汉诺塔问题 1.5猴子吃桃问题 1.6老鼠走迷宫问题 二、递归的时…

从视频中截取gif怎么弄?三步简单完成视频转gif制作

电影、电视剧等短视频充斥着我们的生活,很多小伙伴会将这些视频中的有趣画面提取出来做成Gif动画表情包。那么,怎么才能从视频中提取gif动画呢? 一、使用什么工具才能从视频中提取gif呢? 通过使用GIF中文网这款专业的视频转gif&…

RabbitMQ (工作队列:Work Queues)

本章目录: 什么是Work Queues模拟场景,使用Work Queues官网文档:RabbitMQ tutorial - Work Queues — RabbitMQ 一、何为Work Queues 我们先看下它的结构图 显然,它与入门案例相比只是多了几个消费者。 以下是官方文档说明 In …

【目标检测】目标检测遇上知识图谱:Object detection meets knowledge graphs论文解读与复现

前言 常规的目标检测往往是根据图像的特征来捕捉出目标信息,那么是否有办法加入一些先验信息来提升目标检测的精准度? 一种可行的思路是在目标检测的输出加入目标之间的关联信息,从而对目标进行干涉。 2017年8月,新加波管理大学…

Vue——插槽

目录 插槽内容与出口​ 渲染作用域​ 默认内容​ 具名插槽​ 动态插槽名​ 作用域插槽​ 具名作用域插槽​ 高级列表组件示例​ 无渲染组件​ 插槽内容与出口​ 在之前的章节中,我们已经了解到组件能够接收任意类型的 JavaScript 值作为 props,…

微信小程序 | 基于ChatGPT实现电影推荐小程序

文章目录** 效果预览 **1、根据电影明星推荐2、根据兴趣标签推荐3、根据电影名推荐一、需求背景二、项目原理及架构2.1 实现原理(1)根据用户的兴趣标签(2)根据关联类似主题的题材(3)根据特定的电影明星2.2 …

IK集成ElasticSearch,IK分词器的下载及使用

IK集成ElasticSearch,IK分词器的下载及使用 下载ElasticSearch 8.7.0网址:Download Elasticsearch | Elastic 历史版本地址:Past Releases of Elastic Stack Software | Elastic 解压ElasticSearch 什么是IK分词器 分词∶即把一段中文或…

IO流基础

目录 1.FileOutPutStream字节输入流 1.1FileOutPutStream使用 1.1.1创建对象 FileOutPutStream fos new FileOutPutStream("路径或者File对象"); 1.1.2.写数据 调用write方法,参数是int类型,但传入文件中是asci…

【LeetCode: 剑指 Offer II 112. 最长递增路径 | 递归 | DFS | 深度优先遍历 | 记忆化缓存表】

🍎作者简介:硕风和炜,CSDN-Java领域新星创作者🏆,保研|国家奖学金|高中学习JAVA|大学完善JAVA开发技术栈|面试刷题|面经八股文|经验分享|好用的网站工具分享💎💎💎 🍎座右…

海康工业相机网口相机丢包问题解决方法

​1.1 系统环境设置 1.1.1 网卡设置 网卡推荐使 Intel 芯片的独立千兆网口,例如 intel I350、I210 等芯片组网卡 设置网卡巨型帧为选择 9KB 或 9014 字节 *不同网卡类型,网卡属性有差异,需灵活参考 设置网卡接收与传输缓存区到最大(intel 网卡一般为 2048,realtek 一般…

Program tuning - Druid和Linux配合优化数据库连接池配置

Program tuning - Druid和Linux配合优化数据库连接池配置配置步骤1. 添加依赖2. 添加配置3. 启动监控界面常见问题输入地址之后,浏览器直接打印html代码,而不是登录框刷新页面不能重新加载数据调优步骤1. 开始压测2. 监控线程池状态3. 查看服务器状态结论…

算法 贪心5 || 435. 无重叠区间 763.划分字母区间 56. 合并区间 738.单调递增的数字 968.监控二叉树

435. 无重叠区间 和452. 用最少数量的箭引爆气球 思路是很相似的。本题按照左边排序或者按照右边排序都是可以的,最终目的都是为了让区间尽可能重叠。 1、按右边排序,排序完第一个元素的右边界一定是最小右边界。往下找第一个不与其重合的左边界&#x…