How to print the exact value of a float or double variable which is read by scanf() in C language?

how to print the exact value of a float or double variable which is read by scanf().
for example x=12.333
the o/p should be 12.333 but not 12.333000.
please provide solution to this.

I Suggest you try making a program yourself, If you’re stuck anywhere, share the code and we’ll be happy to help you debug it.

Here is the code,

#include <stdio.h>
int main()
{
    float a;
    double b;
    
    printf("Enter the value of a: ");
    scanf("%f",&a);
    printf("Enter the value of b: ");
    scanf("%lf",&b);
    
    //printing till 2 decimal points
    printf("a = %.2f, b = %.2lf\n",a,b);
    return 0;
}

Output:

Enter the value of a: 123.5678
Enter the value of b: 1234567.8965432
a = 123.57, b = 1234567.90

To print the exact value we use the format %.nf, here n is the number of exact decimal values after point.

1 Like