#include<stdio.h>
int i;
int increment(int i)
{
return ++i;
}
int 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
incrementdoesn’t change anything since the value is passed by value.The local definition of
ishadows the global definition. Therefore, only the local definition ofiis used and the global definition ofiis not affected by the increment which is done on the local copy of the variable.