在 C 中将 char 和 int 相乘第 2 部分
如果我执行以下操作:
int c0 = CHAR_MAX; //8 bit
int c1 = CHAR_MAX; //8-bit
int i = c0*c1; //store in 32-bit variable
printf("%d\n", i); //prints 16129
我们可以看到 8 位数字相乘并产生 32 位输出是没有问题的。
但是,如果我
int i0 = INT_MAX; //32-bit
int i1 = INT_MAX; //32 bit variable
long long int ll = i0*i1; //store in 64-bit variable
printf("%lld\n", ll); //prints 1..overflow!!
在这种情况下,两个 32 位变量相乘,溢出,然后分配给 64 位变量。
那么为什么在乘以整数而不是乘以字符时会发生这种溢出呢?它取决于我的机器的默认字大小吗? (32 位)
If I do the following:
int c0 = CHAR_MAX; //8 bit
int c1 = CHAR_MAX; //8-bit
int i = c0*c1; //store in 32-bit variable
printf("%d\n", i); //prints 16129
We can see that there is no problem with to 8-bit numbers being multiplied together, and producing a 32-bit output.
However, if I do
int i0 = INT_MAX; //32-bit
int i1 = INT_MAX; //32 bit variable
long long int ll = i0*i1; //store in 64-bit variable
printf("%lld\n", ll); //prints 1..overflow!!
In this case, two 32-bit variables were multiplied together, overflowed, and then were assigned to the 64-bit variable.
So why did this overflow happen when multiplying the ints, but not the chars? Is it dependent on the default word-size of my machine? (32-bits)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该更改第二个代码示例
,即在将它们相乘之前,将(至少其中一个)整数转换为 64 位。否则,会发生溢出,因为在将结果分配给
long long
变量之前,尝试将结果存储在int
类型的临时变量中。任何表达式的结果都会转换为其具有最高精度的成员的精度。在第一个示例中,
int
足够大,可以容纳char
相乘的结果,因此不会发生溢出。附带说明一下,不建议将变量命名为
ll
,因为很难区分数字“1”和小写字母“l”。You should change your second code sample like
that is, cast (at least one of the) the ints to 64 bit before multiplying them. Otherwise the overflow happens because the result is attempted to be stored in a temporary of type
int
before assigning it to thelong long
variable. The result of any expression is casted to the precision of its member with the highest precision.In the first example, an
int
is large enough to hold the result of multiplyingchar
s, so there is no overflow.As a side note, naming your variable
ll
is not recommended as it is very difficult to differentiate between the digit '1' and the lowercase letter 'l'.你对正在发生的事情的解释存在逻辑错误。
至少在 Linux 系统上,
CHAR_MAX
当然不是 8 位数字。它是一个(或多或少)简单的预处理器定义,如下所示:因此,对于具有签名 char 的系统,最后两行有效,这意味着当您在代码中写入 CHAR_MAX 时,编译器看到一个普通的 127,其类型为
int
。这意味着乘法
CHAR_MAX
*CHAR_MAX
发生在int
精度。There's a logic fault in your explanation of what is going on.
On at least Linux systems,
CHAR_MAX
certainly isn't an 8-bit number. It's a (more or less) plain preprocessor define, like so:So, for a system with signed
char
s, the two last lines are in effect, which means that when you write CHAR_MAX in your code, the compiler sees a plain 127, which has typeint
.This means that the multiplication
CHAR_MAX
*CHAR_MAX
happens atint
precision.类型转换如何工作...
除非指定显式类型转换,否则任何表达式都会被类型转换为所涉及的最高精度变量/常量的精度。
注意:我没有得到“long long int”部分。也许自从我看到一个以来已经很长了......;-)
你使用的是哪个编译器?
How Typecast works...
Unless explicit typecast is specified, any expression is typecasted to the precision of the highest precision variable/constant involved.
NOTE: I didn't get the "long long int" part. Maybe its been a long time since i saw one... ;-)
which compiler are U using??