开发者

is this no-op or something else?

#include<stdio.h>

int i;
int increment(int i)
{
    return ++i;
}

i开发者_StackOverflownt main()
{
    for(i=0;i<10;increment(i))
    {
        printf("%d",i);
    }
    return 0;
}

Here output is 000000. i.e. infinite lopp occurs.

I want to know that is this occuring due to no-op as we have no variable to store the value of ++i (returned by increment function) or it is due to something else? .please explain.


Yes, it's a no-op. The call to increment doesn't change anything since the value is passed by value.

The local definition of i shadows the global definition. Therefore, only the local definition of i is used and the global definition of i is not affected by the increment which is done on the local copy of the variable.


The variable you are incremented is the local copy of the argument passed to increment. You have named both i so I may not be able to be so clear, but the point is that the i within increment is not the same than the one defined globally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜