Using sprintf and the general syntax "%A.B"
I can do this:
double a = 0.0000005l;
char myNumber[50];
sprintf(myNumber,"%.2lf",a);
Can I set A and B dynamically in the format string?
Using sprintf and the general syntax "%A.B"
I can do this:
double a = 0.0000005l;
char myNumber[50];
sprintf(myNumber,"%.2lf",a);
Can I set A and B dynamically in the format string?
Yes, you can do that. You need to use an asterisk
*
as the field width and.*
as the precision. Then, you need to supply the arguments carrying the values. Something likeNote:
A
andB
need to be typeint
. From theC11
standard, chapter ยง7.21.6.1,fprintf()
function