Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sprintf question

Status
Not open for further replies.

cplot

Programmer
Jan 9, 2004
1
FR
What is the difference between %s, %3s and %.3s ??
Thanks in advance for your response !
 
I hope the syntax is similiar to wsprintf.

%[-][#][0][width][.precision]type

your example touches:
width:
Copy the specified minimum number of characters to the output buffer. The width field is a nonnegative integer. The width specification never causes a value to be truncated; if the number of characters in the output value is greater than the specified width, or if the width field is not present, all characters of the value are printed, subject to the precision specification.

precision:
For numbers, copy the specified minimum number of digits to the output buffer. If the number of digits in the argument is less than the specified precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds the specified precision. If the specified precision is 0 or omitted entirely, or if the period (.) appears without a number following it, the precision is set to 1.

One should not use sprintf or wsprintf cause they are potential security flaws. Buffer Overflows are likely...

If you need more info I could find the msdn page for you describing all the parameters if you program for windows...
 
Why not write a program and play with it to see what the difference is?

For example:
[/code]
char s[10] = "12";
printf(":%s:%3s:%.3s\n");
Code:
Then change the length of your string & see what happens.

You may also want to try things like [code]"%10s","%-10s"

You'll understand it better if you figure it out by trial & error rather than if someone gives you the answer.

Good Luck.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top