K
Kavya
What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
with small example?
Kavya said:What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
Kavya said:What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
They take a pointer to a list of arguments rather than the argumentsWhat is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
OFF TOPIC!!!Hello all,
I'm looking for a sample program in C to print out lines not to the
standard MS Dos Box but into a different control e.g. text control.
Has C the possibility to do printouts to a control instead of using the
MS Dos Box? And what control can I use for this purpose when I need to
have the most similar look to the MS Dos Box?
But since you asked so nicely...Every help is greatly appreciated.
Thanks in advance.
Bill said:They take a pointer to a list of arguments rather than the arguments
themselves. The purpose, if you need it, is to allow you to create an
argument list based on some interactive need, then pass it to the
"v"-printf and -scanf functions.
I actually posted an OFF-TOPIC example recently, using
vsprintf(), that illustrates one "real-world" use:
> [...]
Yeah, and worse, pretty misleading...who wrote that, anyway?Eric Sosman said:The first sentence isn't quite right, and the second is
simply wrong.
The vxxxxx() functions differ from their plain xxxxx()
brethren in one way: Instead of taking a variable number of
arguments they take a fixed number of arguments, one of which
is a va_list.
Whoops, FAQ update needed?This va_list object allows the vxxxxx() function
to access its caller's variable arguments. However, it's not
illuminating to refer to a va_list as "a pointer to a list of
arguments;" a va_list is some kind of compiler magic that may
not involve pointers at all.
C has no way to "create an argument list" other than by
writing a function call in the source code. The call itself
is not in any sense "variable:" if you provide a string and
three int's when you write the call, that call will always
produce an argument list consisting of a string and three
int's. Nothing less, nothing more, and nothing else. Some
systems provide extra magic to allow fancier things, but these
incantations are "C with extras," not C.
Yes.
So, what use are the vxxxxx() functions? They're handy when
you want to write a function that behaves very much like one
of the ordinary xxxxx() functions, but does something a little
bit special. For example, you might want a function that is
"just like" printf() but sends its output to a logging stream
as well as to stdout.
Well, in that case, let me make it off-topic (not to mention theseThe signature should be
int doubleprint(const char *format, ...);
like printf(), but how will you actually write the function?
Do you need to write what amounts to a complete printf() format
interpreter just to implement your special logger?
vprintf() and vfprintf() to the rescue. You write something
like
int doubleprint(const char *format, ...) {
int result;
va_list ap;
/* print to stdout: */
va_start(ap, format);
vprintf(format, ap);
va_end(ap);
/* ... and now to the log: */
va_start(ap, format);
result = vfprintf(logstream, format, ap);
va_end(ap);
return result;
}
(I say "something like" because it's not entirely clear what
value to return if you get an I/O error writing to one stream
while output to the other succeeds -- but that's a separate
issue.) There is no way doubleprint() can pass its own "..."
arguments to printf() or fprintf(), but it *can* latch onto
them with a va_list and pass *that* instead.
Other likely uses: Adding time stamps or similar decorations
to output lines, "printing" to a buffer that will then be handed
to some service C's I/O doesn't support well (GUI dialog boxes,
perhaps), and anything else where you want a capability that is
similar to but a little different from that provided by a
variable-argument function in the Standard C library.
I actually posted an OFF-TOPIC example recently, using
vsprintf(), that illustrates one "real-world" use:
[...]
This is a perfectly typical example of using a "printf-like"
function to create output for a non-printf-able destination (and
it seems adequately topical to me; no need to apologize for using
a vxxxxx() function as it is intended).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.