Predict the output

# include<stdio.h>
int main()
{
	static int i=5;
	if(i--)
	{
		main();
		printf("%d",i);
	}
}

its o/p must be 0000 ,but why it is giving -1-1-1-1-1?

It’s obvious. When i=0 if(i--) registers false and i is still decremented, so i becomes -1, and then the 5 printf s were called.

4 Likes

but when we use if(--i) o/p is 0000?

--i will decrement the value of i , and then return the decremented value.
whereas i-- will decrement the value of i , but return the original value that i held before being decremented.

2 Likes

means condition is checked on the basis of returned value
for if(i--) it is

//i=4 but returns i=5
//i=3 but returns i=4
//i=2 but returns i=3
//i=1 but returns i=2
//i=0 but returns i=1
//i=-1 but returns i=0

for if(--i)

//i=4 but returns i=4
//i=3 but returns i=3
//i=2 but returns i=2
//i=1 but returns i=1
//i=0 but returns i=0

It’s more like the value is checked first and then decremented for i--

1 Like

ok got it, thanks a lot.