本文介绍了解释此C程序的工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请说明每种情况下的工作方式.

Please explain working in each case.

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

案例一:

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;

推荐答案

这些程序均无效.您不能在没有序列号的情况下读取和更新相同的变量.因此,这两个程序都将执行未定义的行为,因此它们可能恰巧在编译器和计算机上返回相同的答案这一事实并不意味着任何事情.

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.

这篇关于解释此C程序的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 00:48