473,327 Members | 1,896 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,327 software developers and data experts.

Multiple Arguements like in printf

Hi,

I'd like to write a function like printf, which takes a string as its
first arguement, but then a variable number of arguements after this,
based on the contents of the first arguement.

How do I do this?

Barry.

Jun 24 '06 #1
5 1731
bg***@yahoo.com schrieb:
I'd like to write a function like printf, which takes a string as its
first arguement, but then a variable number of arguements after this,
based on the contents of the first arguement.

How do I do this?


http://c-faq.com/varargs/index.html
Start from 15.4 and read a little bit further.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 24 '06 #2
<bg***@yahoo.com> wrote in message
Hi,

I'd like to write a function like printf, which takes a string as its
first arguement, but then a variable number of arguements after this,
based on the contents of the first arguement.

The easy, cheating way.

int myprintf(char *fmt, ...)
{
va_list args;
char buff[2048];
int answer;

va_start(args, fmt);
answer = vsprintf(buff, fmt, args);
puts(buff);
va_end(args);
return answer;
}

The real answer

int myprintf(char *fmt, ...)
{
va_list args;
int answer;
int aninteger;
double areal;

va_start(&fmt, args);
while(*fmt)
{
if(*fmt == '%'))
{
fmt++;
if(*fmt == 'd')
{
aninteger = va_arg(args, integer);
printinteger(aninteger);
}
else if(fmt == 'f')
{
areal = va-arg(args, double);
printreal(areal);
}
fmt++;
}
else
{
putc(*fmt);
fmt++;
}
}

va_end(args);
return answer;
}

This is a very cut down printf() since it only handles %d and %f, and I
haven't included the subroutines for converting between numbers and strings.
You can see how with substantial effort you can expand it to include all of
the printf() format specifiers, field widths, and so on.
Jun 24 '06 #3
"Malcolm" <re*******@btinternet.com> writes:
<bg***@yahoo.com> wrote in message
I'd like to write a function like printf, which takes a string as its
first arguement, but then a variable number of arguements after this,
based on the contents of the first arguement.
The easy, cheating way.

int myprintf(char *fmt, ...)
{
va_list args;
char buff[2048];
int answer;

va_start(args, fmt);
answer = vsprintf(buff, fmt, args);
puts(buff);
va_end(args);
return answer;
}

The real answer

int myprintf(char *fmt, ...)
{
va_list args;
int answer;
int aninteger;
double areal;

va_start(&fmt, args);
while(*fmt)
{
if(*fmt == '%'))

[snip] va_end(args);
return answer;
}

This is a very cut down printf() since it only handles %d and %f, and I
haven't included the subroutines for converting between numbers and strings.
You can see how with substantial effort you can expand it to include all of
the printf() format specifiers, field widths, and so on.


Using vsprintf() is relatively easy, but it's not cheating in any
reasonable sense of the term, assuming it's appropriate to your needs.
If vsprintf() does the job, it would be unreasonable *not* to use it.
(If it doesn't, of course, then you'll have to roll your own solution.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 25 '06 #4
In article <L4******************************@bt.com>
Malcolm <re*******@btinternet.com> wrote:

[snippage, with the aim to keep just the va_start() lines plus enough
context for them to make sense]
int myprintf(char *fmt, ...) {
va_list args;
va_start(args, fmt); [and]int myprintf(char *fmt, ...) {
va_list args;
va_start(&fmt, args);


"Two men claim they're Jesus
One of them must be wrong..." --Mark Knopfler

(At least one of the two va_start()s above must be wrong. In fact,
the first one is correct and the second is wrong.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 25 '06 #5
"Chris Torek" <no****@torek.net> wrote
In article <L4******************************@bt.com>
Malcolm <re*******@btinternet.com> wrote:

[snippage, with the aim to keep just the va_start() lines plus enough
context for them to make sense]
int myprintf(char *fmt, ...) {
va_list args;
va_start(args, fmt);

[and]
int myprintf(char *fmt, ...) {
va_list args;
va_start(&fmt, args);


"Two men claim they're Jesus
One of them must be wrong..." --Mark Knopfler

(At least one of the two va_start()s above must be wrong. In fact,
the first one is correct and the second is wrong.)

You're right.
Sorry.
It's ages since I wrote a variadic function.
--
Buy my book 12 Common Atheist Arguments (refuted)
$1.25 download or $7.20 paper, available www.lulu.com/bgy1mm

Jun 25 '06 #6

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

Similar topics

3
by: junk | last post by:
Hi, Given the following function:- void foo (char *fmt, ...) { } I know how to use va_start, va_end etc to process the parameters but is there an easy way of passing the parameter list...
19
by: santosh | last post by:
Hi all, In the following program I allocate a block of pointers to type char, initialised to zero. I then point each of those pointers to a block of allocated memory of fixed size (33 bytes). A...
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
2
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some template function , where EntryType is the input for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.