C 具有不同int类型的操作
我有一个使用多种不同int类型的程序 最常用的是C 具有不同int类型的操作,c,int,size,integer-arithmetic,C,Int,Size,Integer Arithmetic,我有一个使用多种不同int类型的程序 最常用的是uint64\u t和标准int。然而,我想知道我是否能安全地执行它们之间的混合操作 例如,我有一个uint64\u t,我想向它添加一个int,并将该值存储为另一个uint64\u t 这样做安全吗?在对其使用操作之前,是否必须将int强制转换为uint64\u t 我在网上找不到关于它的资料。它可能只是被允许的,没有人质疑它或者我的谷歌查询是错误的 总之,基本上我的问题是,我可以混合使用不同类型的INT进行操作吗?是的,你可以 您的编译器将负责
uint64\u t
和标准int
。然而,我想知道我是否能安全地执行它们之间的混合操作
例如,我有一个uint64\u t
,我想向它添加一个int
,并将该值存储为另一个uint64\u t
这样做安全吗?在对其使用操作之前,是否必须将int
强制转换为uint64\u t
我在网上找不到关于它的资料。它可能只是被允许的,没有人质疑它或者我的谷歌查询是错误的
总之,基本上我的问题是,我可以混合使用不同类型的INT进行操作吗?是的,你可以
您的编译器将负责转换。唯一需要担心的是溢出——如果将结果存储在比输入更小的容器中
您需要的Google搜索术语是“隐式类型转换”——例如,请参阅
该链接包括下表:
算术转换按以下顺序进行:
Operand Type Conversion
---------------------------------------------+--------------------------------------------
One operand has long double type | The other operand is converted to long double type.
---------------------------------------------+--------------------------------------------
One operand has double type | The other operand is converted to double.
---------------------------------------------+--------------------------------------------
One operand has float type | The other operand is converted to float.
---------------------------------------------+--------------------------------------------
One operand has unsigned long long int type | The other operand is converted to unsigned long long int.
---------------------------------------------+--------------------------------------------
One operand has long long int type | The other operand is converted to long long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned long int type | The other operand is converted to unsigned long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int can be |
represented in a long int | The operand with unsigned int type is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int cannot be |
represented in a long int | Both operands are converted to unsigned long int
---------------------------------------------+--------------------------------------------
One operand has long int type | The other operand is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type | The other operand is converted to unsigned int.
---------------------------------------------+--------------------------------------------
Both operands have int type | The result is type int.
---------------------------------------------+--------------------------------------------
是的,你可以
您的编译器将负责转换。唯一需要担心的是溢出——如果将结果存储在比输入更小的容器中
您需要的Google搜索术语是“隐式类型转换”——例如,请参阅
该链接包括下表:
算术转换按以下顺序进行:
Operand Type Conversion
---------------------------------------------+--------------------------------------------
One operand has long double type | The other operand is converted to long double type.
---------------------------------------------+--------------------------------------------
One operand has double type | The other operand is converted to double.
---------------------------------------------+--------------------------------------------
One operand has float type | The other operand is converted to float.
---------------------------------------------+--------------------------------------------
One operand has unsigned long long int type | The other operand is converted to unsigned long long int.
---------------------------------------------+--------------------------------------------
One operand has long long int type | The other operand is converted to long long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned long int type | The other operand is converted to unsigned long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int can be |
represented in a long int | The operand with unsigned int type is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type |
and the other operand has long int type |
and the value of the unsigned int cannot be |
represented in a long int | Both operands are converted to unsigned long int
---------------------------------------------+--------------------------------------------
One operand has long int type | The other operand is converted to long int.
---------------------------------------------+--------------------------------------------
One operand has unsigned int type | The other operand is converted to unsigned int.
---------------------------------------------+--------------------------------------------
Both operands have int type | The result is type int.
---------------------------------------------+--------------------------------------------
C
standard说
如果具有无符号整数类型的操作数的秩大于或等于另一个操作数类型的秩,则具有有符号整数类型的操作数将转换为具有无符号整数类型的操作数类型
因此,由于
int
和unsigned int
具有相同的等级,您可以添加它们,当您添加它们时int
将转换为unsigned int
,将结果再次保留为unsigned intC
标准说
如果具有无符号整数类型的操作数的秩大于或等于另一个操作数类型的秩,则具有有符号整数类型的操作数将转换为具有无符号整数类型的操作数类型
因此,由于int
和unsigned int
具有相同的等级,您可以添加它们,当您添加它们时,int
将转换为unsigned int
,将结果再次保留为unsigned int,最好在算术之前转换为精度更高的类型。不需要转换。C规范说明int将转换为最大类型。讨论转换规则和陷阱。前面注释中的链接现在位于Better,以便在算术之前转换为精度更高的类型。不需要转换。C规范说明int将转换为最大类型。讨论转换规则和陷阱。前面评论中的链接现在位于I see,排名基于大小(“类型”)?I see,排名基于大小(“类型”)?感谢您的解释!我假设UIT64 64是标准的还是长的?取决于无符号long-long的实现?是-像uint64\u t
这样的定义是“已知大小的”,而long
或long
在某种程度上取决于实现。编译器中的头文件将处理这个问题——确保在代码从一个平台移动到另一个平台时,前一种类型是“安全”的。但是出于上述目的,该类型被“翻译”为适当的本机类型,并且通常的规则适用。感谢您的解释!我假设UIT64 64是标准的还是长的?取决于无符号long-long的实现?是-像uint64\u t
这样的定义是“已知大小的”,而long
或long
在某种程度上取决于实现。编译器中的头文件将处理这个问题——确保在代码从一个平台移动到另一个平台时,前一种类型是“安全”的。但出于上述目的,该类型被“翻译”为适当的本机类型,并且通常的规则适用。