Reputation: 4787
i am using sprintf like this
sprintf(cTmpBuf, "%0*lu",targetPrecision,ulFraction);
before this i was using like this
sprintf(cTmpBuf, "%06lu", ulFraction);
now that i know it pick targetPrecision's value and put it to * but i want to know internal thing happen behind it
Upvotes: 0
Views: 741
Reputation: 4785
I'm not sure if this answers your question but in string formatting using *
makes you control the formatting width. So in the first call the formatting width is determined by the value of targetPrecision
.
In the second call the formatting width is fixed to 6 digits.
As for the actual internals of sprintf
. you can refer to @Marcelo Cantos's answer.
Check this link
for formatting parameters.
Upvotes: 0
Reputation: 186098
If you want to know how it's implemented, there's no magic; it's just another argument processed using <stdarg.h>
. Very roughly, it'll be something like:
prec = -1;
⋮
if (*cp == '*') {
prec = va_arg(ap, int);
cp++;
}
Upvotes: 3