Vivek Gaur
Vivek Gaur

Reputation: 4787

Internal working of sprintf

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

Answers (2)

Zaid Amir
Zaid Amir

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

Marcelo Cantos
Marcelo Cantos

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

Related Questions