www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - printf and %.*s

reply "Charlie" <charles jwavro.com> writes:
This abomination needs to be destroyed, or at the very least renamed.  Why
not use one of the unnamed letters instead ?  Does this have some meaning
that im not aware of ?

Charlie
Apr 21 2005
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Charlie" <charles jwavro.com> wrote in message 
news:d48n49$e2m$1 digitaldaemon.com...
 This abomination needs to be destroyed, or at the very least renamed.  Why
 not use one of the unnamed letters instead ?  Does this have some meaning
 that im not aware of ?
Or, native support for printf just needs to be dropped. writef is a wonderful thing..
Apr 21 2005
next sibling parent "Charlie" <charles jwavro.com> writes:
 Or, native support for printf just needs to be dropped.  writef is a
 wonderful thing..
Yea that too. I see alot of newcomers using printf all over the place.
 Does this have some meaning
 that im not aware of ?
Posted too soon, the man pages of printf explain it. ( *nods* to german ben ) Charlie "Jarrett Billingsley" <kb3ctd2 yahoo.com> wrote in message news:d48no9$ei6$1 digitaldaemon.com...
 "Charlie" <charles jwavro.com> wrote in message
 news:d48n49$e2m$1 digitaldaemon.com...
 This abomination needs to be destroyed, or at the very least renamed.
Why
 not use one of the unnamed letters instead ?  Does this have some
meaning
 that im not aware of ?
Or, native support for printf just needs to be dropped. writef is a wonderful thing..
Apr 21 2005
prev sibling parent jicman <jicman_member pathlink.com> writes:
Yep!  Why use printf when writef is D's best way?

:-)


Jarrett Billingsley says...
"Charlie" <charles jwavro.com> wrote in message 
news:d48n49$e2m$1 digitaldaemon.com...
 This abomination needs to be destroyed, or at the very least renamed.  Why
 not use one of the unnamed letters instead ?  Does this have some meaning
 that im not aware of ?
Or, native support for printf just needs to be dropped. writef is a wonderful thing..
Apr 21 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Charlie wrote:
 This abomination needs to be destroyed, or at the very least renamed.
 Why not use one of the unnamed letters instead ?
Because printf is a C library function. There is a similar, more powerful function in D. It's called writef. http://www.digitalmars.com/d/phobos.html#stdio
 Does this have some meaning that im not aware of ?
The notation %.*s takes two pieces of information from the function's arguments: the length of the string and a pointer to the beginning of its data. Basically, if you have something in C like printf(("%.*s", 10, "abcdefghijklmnopqrstuvwxyz"); then it'll print the first 10 characters, i.e. "abcdefghijklmnopqrstuvwxyz". This is because in C, strings are passed as plain pointers. In D, OTOH, strings have length fields and aren't necessarily null terminated. And so when you pass a D string into printf, you are actually passing in both the length and the pointer to the beginning of the data. And so the D statement printf(("%.*s", "abcdefghijklmnopqrstuvwxyz"); is equivalent to the C statement printf(("%.*s", 26, "abcdefghijklmnopqrstuvwxyz"); If you had just "%s" instead of "%.*s", it would try to print the text pointed to by the number 26, which obviously isn't going to work. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Apr 21 2005