From stackoverflow.com
example:
printf("%8.2f",fval);
The field width 8 and precision 2 can be decided during runtime like following:
float fval = 1.23456789;
int width,precision;
scanf("%d %d",&width,&precision);
printf ("%*.*f\n",width,precision,fval);