473,805 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1749
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.co m> 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(an integer);
}
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*******@btin ternet.com> writes:
<bg***@yahoo.co m> 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_Keit h) 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*******@btin ternet.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.n et> wrote
In article <L4************ *************** ***@bt.com>
Malcolm <re*******@btin ternet.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
1755
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 onto another function that also has variable length arguements.
19
3067
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 unique 'string' is then generated using itoa() and rand() for each block of memory. Finally using pointer-to-pointer of type char, I print the contents of the blocks of memory, i.e. the strings, using both printf() and manually, character by...
40
2756
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 name with a space in it) because that filename gets split into 2. I tried
2
1824
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 the template fn 1.. and another type where EntryType and lass P1 are both inputs.. case1 (1)template<class EntryType_> i.e void A<EntryType_>::createObject(EntryType::inputIdType inpId_ )..
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.