- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
K&R 指出,如果其中一个操作数是 int
,则另一个操作数将被转换为 int
。当然,这只是在遵循所有其他规则(如 long double
、float
、unsigned int
等)之后。
根据该逻辑,如果另一个操作数是 int
,则 char
将转换为 int
。但是,如果操作中的最高整数类型是 short
怎么办?
现在,显然我不需要将 char
显式转换为更大的整数,但我确实想知道,ANSI-C 是否处理 char
和short
背后是什么? K&R 对此未作任何说明。
比如说,我有以下几行代码:
char x = 'x';
short y = 42;
short z = x + y;
x
会被转换成short
吗?还是根本就没有转化?
澄清一下:我不是在询问是否或如何将 char
转换为 short
。我只想知道关于隐式类型转换会发生什么。
最佳答案
“整数提升”将在添加之前将它们都转换为 int
:
The following may be used in an expression wherever an int or unsigned int maybe used:
— An object or expression with an integer type whose integer conversion rank is lessthan the rank of int and unsigned int.
[...]If an int can represent all values of the original type, the value is converted to an int;otherwise, it is converted to an unsigned int. These are called the integerpromotions.
(ISO/IEC ISO/IEC 9899:1999 (E),§6.3.1.1)
关于c - 隐式类型转换 : If one operand is short & the other is char, 将 char 转换为 short?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7442081/
C# 程序: short a, b; a = 10; b = 10; a = a + b; // Error : Cannot implicitly convert type 'int' to 'sh
这个问题已经有答案了: Promotion in Java? (5 个回答) 已关闭10 年前。 我有三个短变量。当我将两个相加并将结果分配给第三个时,Eclipse 告诉我需要将其转换为 Short
我正在开展一个项目,我需要获取一系列带符号的 16 位整数、负值和正值,并将它们发送到函数以在单元测试期间进行分析。 由于不同的原因,该函数仅采用无符号 16 位整数数组,因此我需要将有符号整数存储在
这个问题在这里已经有了答案: Implicit type promotion rules (4 个答案) 关闭 3 年前。 有如下代码: short a = 5; short b = 15; sho
我有以下代码: var v = [0xFF, 0xFF]; alert((v[0]> 16); 16位左移将所有位向左移动;算术 16 位右移在移位时处理符号。 (Javascript 使用 32 位
我正在尝试尽可能多地挤出我的内存。我有一个 4.9999995e13 整数矩阵,但它们只需要为真或假 - 基本上我只需要为这些整数中的每一个存储一位。 我知道 C 中没有单个位类型(也许有人可以向我解
这个问题已经有答案了: Different between parseInt() and valueOf() in java? (11 个回答) 已关闭 5 年前。 我最近偶然发现了another q
当我将相同的值分配给有符号和无符号的 short 并进行比较时,它失败了,但它适用于 int。除非我强制转换一个或另一个使它们成为相同的类型,否则比较不起作用。 #include int main()
所以我试图解释以下输出: short int v = -12345; unsigned short uv = (unsigned short) v; printf("v = %d, uv = %u\n
当我将相同的值分配给有符号和无符号的 short 并进行比较时,它失败了,但它适用于 int。除非我强制转换一个或另一个使它们成为相同的类型,否则比较不起作用。 #include int main()
这个问题在这里已经有了答案: What is the 'short' data type in C? (1 个回答) 关闭 7 年前。 将变量声明为 short int 和 short 有什么区别?
C 中的 short int 包含 16 位,第一位表示该值是负数还是正数。我有一个 C 程序如下: int main() { short int v; unsigned short
Short.parseShort(String s, int radix) 存在,Integer.parseInt(String s, int radix) 和 Long.parseLong(Stri
我想我可以从 this question 推断出来但我不能 我当然可以 short[] shortarray = {0,1,2}; List shortList = new ArrayList();
这个问题在这里已经有了答案: char and the usual arithmetic conversion rules (5 个答案) 关闭 6 年前。 假设 int 是 16 位类型,以防止从
到目前为止我已经试过了,但我仍然在下面的代码中遇到错误: #include typedef unsigned short unichar; typedef const unichar unimap_t
unsigned short int 和 c 中的 unsigned short decleration 之间有区别吗?如果有,那是什么?我尝试在网上查找,但找不到任何有值(value)的东西。 un
我有数据库,由于与另一位开发人员混淆,我需要在一个表中更改列的值。我有一个名为 credits 的表和一个名为 credit_type 的列。 credit_type 有 2 个值 long 和 sh
我在 JNI hell 中进行类型转换: 这是一般的流程: 读取一个文件,它返回一个一维 float 组。 转换这些floats[] 到 shorts[](*4095,我想要一个12位数字) 将这些短
我正在尝试优化两个 c 样式数组的点积,这些数组的大小为 contant 和 small,类型为 short。 我已经阅读了一些关于 SIMD 内在函数的文档以及许多关于使用此内在函数进行点积优化的博
我是一名优秀的程序员,十分优秀!