Anyone knows if it is possible to use printf to print a VARIABLE number of digits?
The following line of code prints exactly 2:
printf("%.2lf", x);
but let's say I have a variable:
int precision = 2;
Is there a way to use it in printf to specify the number of digits?
Otherwise I will have to write a 'switch' or 'if' structure.
Thanks
It is possible:
#include <stdio.h>
int main() {
int precision = 3;
float b = 6.412355;
printf("%.*lf\n",precision,b);
return 0;
}
Yes, you can do that easily -
int precision = 2;
printf("%.*lf", precision, x);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With