什么是preFIX和后缀运营商之间的区别? [英] What is the difference between prefix and postfix operators?

查看:144
本文介绍了什么是preFIX和后缀运营商之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下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屋!

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