473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

vscanf and vprintf

What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?

Nov 18 '06 #1
5 8604
Kavya wrote:
What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
See below, (read the copyright notice too):

<http://www.dinkumware.com/manuals/?manual=compleat&page=stdio.html#vprintf>

Nov 18 '06 #2
Kavya wrote:
What is the basic purpose of vscanf and vprintf? Can someone explain
with small example?
Also see:

<http://c-faq.com/varargs/vprintf.html>
<http://c-faq.com/varargs/vscanf.html>

Nov 18 '06 #3

Kavya <Le******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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 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:

From: Bill Reid <ho********@happyhealthy.net>
Newsgroups: comp.lang.c
Subject: Re: Output to a control instead of the MS Dos box for C programs
Date: Tuesday, November 07, 2006 1:58 PM

tizi_de <ti*************@web.dewrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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?
OFF TOPIC!!!
Every help is greatly appreciated.
Thanks in advance.
But since you asked so nicely...

....

So if I compile the thing as a Windows GUI application,
the pre-processor turns all occurences of printf() into
gui_printf(), which is declared in "p_output.h" and defined
in a file called "p_output.c" which is linked into your
program.

gui_printf() is declared with the exact same "signature"
as "printf()", and defined as follows:

int gui_printf(char *format,...) {
unsigned print_length;

p_line_buffer[0]=NUL;
va_list ap;

va_start(ap,format);
vsprintf(p_line_buffer,format,ap);
va_end(ap);

print_length=(int)strlen(p_line_buffer);

print_line_buffer();

return print_length;
}

Note that the trick here is the use of the "vsprintf()"
function to handle all the functionality of "printf()" without
the nasty consequence of actually sending the output to
the "console". Instead, the formatted output is "printed"
to a buffer, and then the buffer is "printed" to the
Windows control by the "print_line_buffer()" function.

....

---end of archived excerpt

Despite any debate about the topicality or advisability of doing
the above, note that you have to follow all the rules of creating
a "variadic" argument list, using va_start() and va_end(), to use
the "v" functions:

va_start(ap,format);
vsprintf(p_line_buffer,format,ap);
va_end(ap);

So what you do is build up a "format" string interactively as
needed (such as "Here's some stuff: %s %d %d %d", whatever),
then when you want to printf() or scanf(), call va_start() to "start"
the formatting of the variadic list, call your "v" function with
your "format" string, then call va_end().

---
William Ernest Reid

Nov 18 '06 #4
Bill Reid wrote:
Kavya <Le******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...

>>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 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.
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. 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.

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. The 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).

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 18 '06 #5

Eric Sosman <es*****@acm-dot-org.invalidwrote in message
news:Ot******************************@comcast.com. ..
Bill Reid wrote:
Kavya <Le******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>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 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.

The first sentence isn't quite right, and the second is
simply wrong.
Yeah, and worse, pretty misleading...who wrote that, anyway?
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.
In one sense, they move the "variadic" list processing out of the
printf or scanf functions into the calling function where it can be
used for several purposes as you note below...
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.
Whoops, FAQ update needed?

comp.lang.c FAQ list · Question 15.5
Q: How can I write a function that takes a format string and a variable
number of arguments, like printf, and passes them to printf to do most
of the work?

A: Use vprintf, vfprintf, or vsprintf. These routines are like their
counterparts printf, fprintf, and sprintf, except that instead of a
variable-length argument list, they accept a single va_list pointer.

---end of FAQ excerpt

And update just about every "popular" piece of documentation on the
topic...for example, from my development package non-documentation:

The v...printf functions are known as alternate entry points
for the ...printf functions. They behave exactly like their ...printf
counterparts, but they accept a pointer to a list of arguments
instead of an argument list.

---end of non-documentation excerpt

Of course, in all cases, the argument is clearly not declared as a
pointer, so this confutzion probably arises from the way that va_arg
processes the list, or something like that...
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.
Yeah, the typical "professional" uses I've seen usually are handling
some type of multiple formatting and output of error conditions...
>The 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).
Well, in that case, let me make it off-topic (not to mention these
functions are only "standard" in the largely unused "C99"):

void print_console_buffer(unsigned new_line) {

TextOutForm->TextOutput->Lines->Strings[printed_lines]=newline;

if(new_line) {

...

}

void print_line_buffer(void) {
unsigned char_idx=0;

while(char_idx<LINEMAX) {
if(next_char==console_width) {
newline[next_char]=p_line_buffer[char_idx];
newline[next_char+1]=NUL;
next_char=0;
print_console_buffer(NEWLINE);
}

else if(p_line_buffer[char_idx]=='\n') {

...

}

int gui_printf(char *format,...) {
int print_length;

p_line_buffer[0]=NUL;
va_list ap;

va_start(ap,format);
vsprintf(p_line_buffer,format,ap);
va_end(ap);

print_length=(int)strlen(p_line_buffer);

print_line_buffer();

return print_length;
}

---
William Ernest Reid

Nov 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Johan | last post by:
Hi, Does anyone know how to use the vprintf ( variable arguments ) routine on c++ stream (ofstream for example ). Is there some class / library you can use ? regards Johan
3
by: Douwe | last post by:
I try to build my own version of printf which just passes all arguments to the original printf. As long as I keep it with the single argument version everything is fine. But their is also a version...
8
by: Joe | last post by:
Hi, The declaration of qsort is: void qsort(void *base, size_t count, size_t size, int (*comp)(const void *e1, const void *e2)); Since const is used in the declaration of the comparison...
62
by: Juuso Hukkanen | last post by:
I am looking for a wish list of things which should be removed from the C (C99) - due to feature's bad security track record <OT>or Multithreading unsafety. I need this list for a project intending...
2
by: david wolf | last post by:
My understanding is that cstdio basically is the same as stdio.h except the functions are in a namspace called std. However when I take a look at the content of the file cstdio, it has the...
1
by: braj | last post by:
Hi Friends, I was trying a test code to use the icu stuff with variable arguments and I seem to be heading nowhere. Can anyone please help. Given below is the test code and the Output. ...
8
by: Fred | last post by:
I've got following program encapsuled fscanf, however, it doesn't work. I'm sure that the content format in "a.txt" is OK, the content would be correctly read if using fscanf directly, what's...
10
by: raphfrk | last post by:
Is there any way to pass a variable list to a function, so as to act as a passthrough? for example: void new_printf ( char *buf , ... ) { va_list vlist; <modify buf slightly>
1
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Nov 10, 12:08 pm, maverik <maverik.m...@gmail.comwrote: Sorry but I don't see how you can deduce that, I don't think that excerpt clarifies whether the "va_arg" calls made inside vfprintf...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.