Home / Programs / Write a program that illustrates the run time field width and precision adjustment while using printf( ).
Programming Example

Write a program that illustrates the run time field width and precision adjustment while using printf( ).

👁 1,436 Views
💻 Practical Program
📘 Step by Step Learning
Write a program that illustrates the runtime field width and precision adjustment while using printf( ).

Program Code

/*
 Program:  Write a program that illustrates the run time field width and
  precision adjustment while using printf( ).  
  
 Author: www.atnyla.com  
 
*/ 

#include "stdio.h" 
int main()
{
  double x=1234.567890;
  int i=8,j=2;

  while(i<12)
  {
   j=2;
   while(j<5)
   {
     printf("width = %2d precision = %d display >>%*.*lf<<\n",i,j,i,j,x);
     j++;
   }
   
   i++;
   }
  return 0;
}

Output

width =  8 precision = 2 display >> 1234.57<<
width =  8 precision = 3 display >>1234.568<<
width =  8 precision = 4 display >>1234.5679<<
width =  9 precision = 2 display >>  1234.57<<
width =  9 precision = 3 display >> 1234.568<<
width =  9 precision = 4 display >>1234.5679<<
width = 10 precision = 2 display >>   1234.57<<
width = 10 precision = 3 display >>  1234.568<<
width = 10 precision = 4 display >> 1234.5679<<
width = 11 precision = 2 display >>    1234.57<<
width = 11 precision = 3 display >>   1234.568<<
width = 11 precision = 4 display >>  1234.5679<<
Press any key to continue . . .

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.