inc/dec operators with scanf
Hi everyone! When I give the value for computation in the program it gives me the right answer,but when I use scanf and enter the value,the result is incorrect. I am using gcc compiler.
Code:
//program without scanf
#include<stdio>
int main()
{
int x=2,y;
y=x++ + ++x;
printf("x=%d,y=%d\n");
return 0;
}
Output: x=4,y=6 // correct output
//program with scanf
#include<stdio>
int main()
{
int x,y;
printf("value for x\n");
scanf("%d",&x);
y=x++ + ++x;
printf("x=%d,y=%d\n");
return 0;
}
Output: x=3,y=5 //incorrect output
I am not able to find a logical explaination,could anyone help me out here?