Home C Programming Language / 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,437 Views
💻 Practical Program
📘 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;
}


                        

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.