William M. answered 12/21/19
STEM Tutor (Ph.D, Northwestern) | Algebra–Calc, Bio, Chem, Physics, CS
You can use either %g or %f for both floating point numbers and for doubles.
The difference is in the precision with which each prints:
%.3g means you want the total number of significant digits to be 3
%.3f means you want 3 digits after the decimal.
What if you just say %g or %f?
%g will get you 6 sig figs, by default
%f will get you 6 digits after the decimal, by default
Here's the code..............................................
int main(int argc, const char* argv[]) {
double temp_cels = 1234.5678;
printf("temp is: %.3f C, or using sig figs, is: %.3g C\n",
temp_cels, temp_cels);
printf("default case\n");
printf("temp is: %f C, or using sig figs, is: %g C\n",
temp_cels, temp_cels);
return 0;
}
//--------------------------------------------OUTPUT
temp is: 1234.568 C, or using 3 sig figs is: 1.23e+03 C
default case...
temp is: 1234.567800 C, or using default sig figs is: 1234.57 C
Hope that helps!