Below is my code and program output:
#include <stdio.h>
int main()
{
float salary;
printf("Please enter the salary : ");
scanf("%f", &salary);
printf("Your Salary is : %f\n", salary);
}
desktop:~$ ./a.out
Please enter the salary : 101.8889999
Your Salary is : 101.889000
desktop:~$
I tried many ways by entering random decimal values but sometime it printed correct values (as entered during scanf call) and sometimes it just prints something near to it. Why is it that the float variable values are not printing correct values as entered?
As per example above - if I enter 101.8889999 then the value saved in salary float variable is 101.889000?
I tried changing the last line as :
printf("Your Salary is : %10.9f\n", salary);
But still it gave below output:
Please enter the salary : 101.889000
Your Salary is : 101.888999939
Please enter the salary : 10.999999999
Your Salary is : 11.000000000
Which is the correct value to print float using printf and is it that float values may / may not be stored as exact values as entered in float variables?