I have the code below

#include <stdio.h>

int main(){

    float n;
    scanf("%f",&n);
    printf("%f",n);

return 0;
}

Then, I run the code
input: 45321.56 is not same with
output:45321.558594

I want to get output which same as the input.
How to solve this problem?

Thank you

I want to get output which same as the input.

Then use strings. Floating-point is inherently imprecise due to the limitations of the machine and gains or losses from rounding. It's unreasonable to expect the internal representation of a value to be exactly what the user typed for input, especially given that printf() does its own rounding when converting that value back to a string. In this specific case you can normalize the input and output by forcing a precision to the input:

printf("%.2f\n", n);

But that doesn't help if the input changes to use 3 characters past the radix, you'd see the same problem in reverse.

So, it means impossible to get the same value?
How if I change the data type become double?

So, it means impossible to get the same value?

Using float?? no! (reason:as said by deceptikon)

How if I change the data type become double?

yes, you'll get same value if you use double.

yes, you'll get same value if you use double.

I really wish people would learn the details before regurgitating bad information all over these forums.

No, double will not correct the problem. Float and double both have the same inexactness problem, just at different levels. It cannot be solved. It simply has to be understood and dealt with.

haha.. as if i didn't know, that double is an extension of float. :P
i executed the program with double and in all possible cases. i got same ans but I don't know why it happens.. But, as far as answer's concern, we get the same value printed.
with due respect sir waltP,
I'm not giving any wrong information. I never said that using double will solve the problem. I said that it gives the same value. and as i mentioned before, I don't know why it happens

I never said that using double will solve the problem.

Yes you did.

You said "yes, you'll get same value if you use double." That implies that the problem was solved, which is wrong.

If you didn't mean that, you should have expounded on your information.

This question has already been answered. Start a new discussion instead.