Printing with Field Widths Write a program to test the resul

Printing with Field Widths: Write a program to test the results of printing the integer value 12345 and the floating – point value 1.2345 in various-sized fields. What happens when the values are printed in fields containing fewer digits than the values?

Solution

#include <stdio.h>

int main()
{
int i=12345;
float j=1.2345;
// for Integer sized variable
printf(\"%d\",i); //printing int value in integer sized variable
printf(\"\ %f\",(float)i); //printing int value in float sized variable
printf(\"\ %lf\",(double)i); //printing int value in double sized variable
  
// for float sized variable
printf(\"\ %d\",(int)j); //printing float value in integer sized variable
printf(\"\ %f\",(float)j); //printing float value in float sized variable
printf(\"\ %lf\ \",(double)j); //printing float value in double sized variable
return 0;
}

Output :

12345   

12345.000000   

12345.000000

1

1.234500 1.234500

When the values are printed in fields containing fewer digits than the values , there is loss of range and precision in type variable values.

eg.

float j=1.2345;

printf(\"\ %d\",(int)j);

Output :

1 (Because there is loss in range and precision)

Printing with Field Widths: Write a program to test the results of printing the integer value 12345 and the floating – point value 1.2345 in various-sized field

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site