解释这个c程序的输出.... [英] Explain the output of this c program....

查看:67
本文介绍了解释这个c程序的输出....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<conio.h>
#include<stdio.h>
int main()
{

int a[5]={5,1,15,20,25};
int i,j,m;
i=++a[1];
j=a[1]++;
m=a[i++];
printf("%d %d %d",i,j,m);
return 0;
    }
output:3,2,15

推荐答案

有什么问题?

这完全取决于运算符优先级和事件发生的顺序。



因此请使用调试器,并查看每个阶段的确切内容。 />
然后看看操作员,这应该是显而易见的。



这是你的作业,而不是我的作业 - 你会通过做更好的学习你自己!
What's the problem?
It's all down to operator precedence and the order in which things happen.

So use your debugger, and look at exactly what you get at each stage.
Then look at the operators and it should be obvious.

This is your homework, not mine - and you will learn a lot better by doing it yourself!


最初:

a [0] = 5

a [1] = 1

a [2] = 15

a [3] = 20

a [4] = 25

i = 0

j = 0

m = 0



之后i = ++ a [1]

a [1] = 1 + 1 = 2

i = a [1] = 2



之后 j = a [1] ++

a [1] = 2 + 1 = 3

j = 2(post -increment:j在递增之前被赋值为[1]的值。



之后m = a [i ++]

i = 2 + 1 = 3

m = a [2] = 15



结果:

i = 3

j = 2

m = 15
Initially:
a[0] = 5
a[1] = 1
a[2] = 15
a[3] = 20
a[4] = 25
i = 0
j = 0
m = 0

After i = ++a[1]:
a[1] = 1 + 1 = 2
i = a[1] = 2

After j = a[1]++:
a[1] = 2 + 1 = 3
j = 2 (post-increment : j is assigned the value of a[1] before it is incremented)

After m = a[i++]
i = 2 + 1 = 3
m = a[2] = 15

Result:
i = 3
j = 2
m = 15


知道这一点, ++预增量 &安培; ++后增量



i = ++ a [1];

a [1]为1,预先递增为2,该值存入i。



j = a [1] ++;

2存储在j中,然后a [1]后递增到值3.



m = a [i ++];

a [2],15存入m,我被后递增到3.



-KR
Know this, ++a pre-increment & ++a post-increment

i = ++a[1];
a[1] is 1, is pre-incremented to 2 and that value is stored into i.

j = a[1]++;
2 is stored into j and then a[1] is post-incremented to the value 3.

m = a[i++];
a[2], 15 is stored into m, and i is post-incremented to 3.

-KR


这篇关于解释这个c程序的输出....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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