什么是preFIX和后缀运营商之间的区别? [英] What is the difference between prefix and postfix operators?
问题描述
以下code打印的9.为什么值?在这里,收益率(我++)
将在11日和到期返回一个值 - 我
的值应该是10本身,任何人都可以解释这是如何工作?
#包括LT&;&stdio.h中GT;
主要()
{
INT I =乐趣(10);
的printf(%d个\\ N - 我);
}INT乐趣(int i)以
{
回报(我++);
}
在在函数返回I
是因为你使用的是修正后的运营递增(++) 。无论如何,的增量I
不是全球性 - 只各自的功能。如果你已经使用了pre-固定运营商,这将是 11
,然后递减到 10
。
所以,你再返回 I
10,并在printf函数,这说明 9
不减少它 10
像你想象的。
The following code prints a value of 9. Why? Here return(i++)
will return a value of 11 and due to --i
the value should be 10 itself, can anyone explain how this works?
#include<stdio.h>
main()
{
int i= fun(10);
printf("%d\n",--i);
}
int fun (int i)
{
return(i++);
}
The function returns before i
is incremented because you are using a post-fix operator (++). At any rate, the increment of i
is not global - only to respective function. If you had used a pre-fix operator, it would be 11
and then decremented to 10
.
So you then return i
as 10 and decrement it in the printf function, which shows 9
not 10
as you think.
这篇关于什么是preFIX和后缀运营商之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!