C
Chris M. Thomasson
Richard Harter wrote: [...]
It would be useful if there were a sprintf variant that does not
require the user to supply a buffer but which takes care of
allocation and returns a pointer to the resulting formatted
string.There is, it's called snprintf.
I believe it's called `asprintf'
asprintf isn't available on all compilers.
Yeah, I know. Too bad it's not. Oh well, shi% happens.
C isn't portable. To write
portable C, you have to reinvent the wheel. To reinvent the wheel
properly, you need to be able to write English in addition to
effective C, and very few people here have the ability to construct a
complex sentence.
To construct an asprintf, you must construct it on top of an snprintf
if you're using a C99 compliant compiler, or else either recreate the
logic of printf. You have to scan for percents and sum the length of
strings and determine the string length of numbers. I suppose you
could use Malcolm's value of 30.
You then have to stress test.
Well, you don't really have to recreate `printf()'. You can use the hack
code I presented here:
http://clc.pastebin.com/f31279cba
It's certainly easier than recreating `printf()'. Whether or not it's
actually practical, well, that another matter.
To use the "hacks" presented here, you have to read the code line by
line to make sure the hacker hasn't fucked up.
;^)
As a personal matter, I
don't like to read most code written by others, since their aesthetic
is usually not mine (I found John Nash's code most pleasant on the
other hand). I'd rather code my own, with longer identifiers and
overall a more elegant way of expression...not that it's perfect and
bug free.
C is great in worlds without strings. That would be an operating
system kernel. If you want to write C, learn how to develop operating
systems.
Well, I have to use a low-level language like C for some things that are not
related to kernel programming.
Don't mess up military veterans' benefits, small businesses,
or schools with this language.
Yikes!