When I was doing the practice questions today, I found that the outputs of printf("%d\n",x--); and printf("%d\n",x); are the same.
I changed it to printf("%d\n",x++); and found to be the same. I want to know why.
#include<stdio.h>
int main()
{
int x;
scanf_s(" %d", &x);
if (x++ > 5)
printf("%d",x);
else
printf("%d\n",x--);
return 0;
}
You used the post-decrement/-increment operators.
x--decrementsxand returns the original value ofx.x++incrementsxand returns the original value ofx.To get the desired behaviour, use the pre-decrement/-increment operators.
--xdecrementsxand returns the modifiedx.++xincrementsxand returns the modifiedx.