I am taking numerical string as an input from user and than converting it to float with the help of sscanf()
function :
#include<stdio.h>
float getfloat(void);
int main()
{
float a;
a=getfloat();
printf("You entered %f\n",a);
return 0;
}
float getfloat(void)
{
float n;
char nstr[50];
printf("Enter a numeric string\n");
gets(nstr);
sscanf(nstr,"%f",&n);
return n;
}
Now let's suppose I enter a string 100.1234
. After converting to float with help of sscanf
when i print the float value i am getting 100.123398
as stored value. Now these are very close values but why am i not getting the same result as i entered. What is going on inside sscanf
?