printf -*s format string 
Author Message
 printf -*s format string

What's the difference between the following two format strings?

Here, it says "%-*s":
printf ("%s%-*s[%s]\n", prompt, 3, "", defvalue);

Here, it says "%*s":
printf ("%s%*s[%s]\n", prompt, 3, "", defvalue);

They both output the same.....



Sat, 26 Mar 2005 15:20:15 GMT  
 printf -*s format string

comp.lang.c:

Quote:
> What's the difference between the following two format strings?

> Here, it says "%-*s":
> printf ("%s%-*s[%s]\n", prompt, 3, "", defvalue);

> Here, it says "%*s":
> printf ("%s%*s[%s]\n", prompt, 3, "", defvalue);

> They both output the same.....

The flag `-' means that the field is left-justified. In your case,
it means that the first printf puts the empty string to the left
of the 3 padding spaces, and the second printf puts the empty
string to the right of the 3 padding spaces.

Jonas

--
Use spaces instead of tabs.



Sat, 26 Mar 2005 18:16:17 GMT  
 printf -*s format string

Quote:
> What's the difference between the following two format strings?

> Here, it says "%-*s":
> printf ("%s%-*s[%s]\n", prompt, 3, "", defvalue);

> Here, it says "%*s":
> printf ("%s%*s[%s]\n", prompt, 3, "", defvalue);

> They both output the same.....

The '-' tells printf to left justify the string in the 3 character
wide field. As the string passed to %s is an empty one you'll not
see any difference, try:

printf ("%s%-*s[%s]\n", prompt, 3, "x", defvalue);
and
printf ("%s%*s[%s]\n", prompt, 3, "x", defvalue);

and you'll probably recognize the difference.
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 26 Mar 2005 18:21:20 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. STL strings & printf-type formatting

2. Source to format strings like printf()

3. printf Format string

4. printf() format-string parser wanted!

5. Printf format strings

6. printf and variable length string format (asterisk)

7. placing output of printf in memory: formatting strings

8. printf and formatting numeric strings

9. Examples of format strings for scanf/printf ?

10. printf format string: how bad style is this?

11. Format string as a parameter to printf()?

12. replacing printf with my printf, then calling C lib's printf

 

 
Powered by phpBB® Forum Software