解释一下这个C程序的工作原理

发布于 2024-10-21 12:13:48 字数 564 浏览 4 评论 0原文

请解释每种情况下的工作情况。

为什么两种情况的输出相同?

情况一:

int main (void)
{
    int i = 5;
    if(i == ++i)           //plz explain here How values are checked
       printf("Equal");
    else
      printf("Not Equal");

  return 0;
}
//Output: Equal;

情况二:

int main (void)
{
    int i = 5;
    if(++i == i)            //plz explain here How values are checked
       printf("Equal");
    else
      printf("Not Equal");

  return 0;
}
//Output: Equal;

Please explain working in each case.

Why both cases have same output?

Case I:

int main (void)
{
    int i = 5;
    if(i == ++i)           //plz explain here How values are checked
       printf("Equal");
    else
      printf("Not Equal");

  return 0;
}
//Output: Equal;

Case II:

int main (void)
{
    int i = 5;
    if(++i == i)            //plz explain here How values are checked
       printf("Equal");
    else
      printf("Not Equal");

  return 0;
}
//Output: Equal;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零崎曲识 2024-10-28 12:13:48

这些程序都无效;如果中间没有序列点,则不允许读取和更新同一变量。因此,这些程序将执行的操作都是未定义的行为,因此它们可能碰巧在您的编译器和机器上返回相同的答案这一事实并不意味着任何事情。

Neither of those programs is valid; you are not allowed to read and update the same variable without a sequence point in between. Thus, it is undefined behavior what either of those programs will do, and so the fact that they may happen to return the same answer on your compiler and machine doesn't mean anything.

木落 2024-10-28 12:13:48

它是相等的,因为这是 C。正如另一个答案所说,这个操作的结果是未定义的,因为你违反了 C 规则——这意味着你不能保证在切换编译器时得到相同的答案(尽管所有编译器可能实现相似,这并不构成保证)。 C 允许你搬起石头砸自己的脚,但这并不意味着这样做是好的做法。

现在,为什么它有效?

猜想#1:

i 可能存储在寄存器中,例如r1,并且编译器可以使用自动增量寻址模式将整个比较编译成单个 CMP 指令。假设它是 CMP ++r1, r1CMP r1, ++r1,那么根据实际的 CPU,两者都可能返回 true 比较。

猜想#2:

编译器可能会将比较编译为:

inc r1   // increment r1
CMP r1, r1   // compare with itself

猜想#3:

编译器可能正在优化以始终将简单的变量访问放在右侧。允许这样做是因为 == 运算符中的执行顺序未定义,并且编译器可以根据需要重新排列内容。

It is equal because this is C. As the other answer says, the result of this operation is undefined because you violate C rules -- which means that you cannot guarantee the same answer when switching compilers (although all compilers may be implemented similar, which doesn't make it a guarantee). The fact that C allows you to shoot yourself in the foot does not mean that it is good practice to do so.

Now, why does it work?

Conjecture #1:

i may be stored in a register, say r1, and the compiler may be compiling this entire comparison into one single CMP instruction, with an autoincrement addressing mode. Say it is CMP ++r1, r1 or CMP r1, ++r1, then depending on the actual CPU, both may return a true compare.

Conjecture #2:

The compiler may be compiling the compare into:

inc r1   // increment r1
CMP r1, r1   // compare with itself

Conjecture #3:

The compiler may be optimizing to always put a simple variable access to the right hand side. It is allowed to do that because the order of execution in a == operator is undefined and the compiler can rearrange things as it likes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文