如何确定我+++ - J用C [英] How to determine i+++--j in C

查看:153
本文介绍了如何确定我+++ - J用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,

int i = 20;
int j = 5;
int k = i+++--j;

为什么K = 24?

Why is k=24?

在我的理解中,k =(I)+ +(--j),所以它是(20 + 4)+ = 25。

In my understanding, k = (i)++ + (--j) so it is (20 + 4)++ = 25.

确定。这里是一个小程序,我写测试,是的K指定后后增量就完成了。

OK. Here is a little programme I wrote for test, and yes the post increment is done after k is assigned.

#include <stdio.h>
int main()
{
    int i = 20;
    int k = i++;
    printf("%d\n", k);
    printf("%d\n", i);
    return 0;
}

输出:

20
21

谁能告诉我为什么否决?我不能确定这一点,因为我是一个新得到商业为C。

Could anyone tell me why vote down? I was unsure about this because I was a new commer to C.

推荐答案

把它看成一个贪心算法后,将采取尽可能多的字符可能的,如果他们是有意义的。

Think of it as a greedy algorithm, it will take as many characters as possible, if they make sense.

例如:

i+   // good, keep going
i++  // good, keep going
i+++ // not good, start new token
+    // good, keep going
+-   // not valid, start new token
-    // good
--   // good
--j  // valid

所以:

int i = 20;
int j = 5;
int k = i++ + --j; // (20++) + (--5)

这是它是如何进行分组。第二部分是pre和后递增。

That is how it is grouped. The second part is pre and post increment.

i++ // post-increment, use the value first and then increment
--j // pre-increment, decrement first and then use the value

所以,你得到:

int k = 20 + 4
// afterwards: i = 21 and j = 4

这篇关于如何确定我+++ - J用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆