c - 将2个字符加在一起时出现问题
问题描述
char c = '2';
char d = '3';
char e = 'c' + 'd';
int digit = e - '0'; // ACSII into int
printf("digit = %d \n", digit); //should display 23
printf("char c : %c \n",c); //should display 2
printf("char d : %c \n",d); //should display 3
我要做的是在不使用 strcat() 函数将 23 显示为 int 的情况下进行字符串连接。
但是我似乎得到了:
数字 = - 105
解决方案
溢出!Achar
只有8位长。默认情况下,它是有符号的,所以范围是-128 <= x < 128
. 您正在添加字符'c'
and 'd'
(不是变量c
and d
),这意味着您实际上是在添加 ASCII 值,因此e
value也是如此199
。因为是签名的,所以真的199 - 256 == -57
。然后减去ASCII 码'0'
,48
得到-57 - 48 = -105
.
推荐阅读
- sql-server - 使一列的数据类型成为另一列的类型?
- opengl - 使用 SharpGL 绘制光标位置的延迟
- flutter - 在饼图颤动中更改值时出现问题
- oracle-cloud-infrastructure - 如何在 oracle oci java sdk 中运行初始化脚本来创建 vm
- huawei-mobile-services - 调用AchievementsClient.getAchievementList接口获取成就返回结果码7002
- c++ - 如何捕获引发硬件中断的异常?
- php - 使用 Redis 和 MySQL 加载和更新数据的最佳实践是什么?
- c# - 如何绑定 ObservableCollection 中所有项目的 Time-Dependent-Property
到一个文本框? - html - 如何将 HTML 小部件的值从 HTML 表单传递到 eXist-db 中的 Xquery?
- julia - 不使用 JuMPeR 是否可以建立相关的鲁棒优化模型