delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/19/20:33:53

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: making a printf type function
Date: Sun, 19 Jul 1998 20:25:54 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 54
Message-ID: <35B28E92.885FD06C@cs.com>
References: <35b2439f DOT 0 AT news DOT provide DOT net>
NNTP-Posting-Host: ppp128.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Sean Middledich wrote:
> 
> I was wondering how to the ... to make a printf type function.  My current
> project uses a screen buffer, but the function I use to write strings to the
> buffer can only use one string at a time.  If I wanted to do a porintf type
> effect, I have to do this:
> 
> char Text[81];
> 
> sprintf ( Text, "String A: %s, Number A: %d", StringA, NumB );
> Print ( Text );
> 
> I would love to take out the business with Text[81] and sprintf.  I just
> need to know how to use the ... ( elipse was it called? ).

There are a set of ANSI standard C functions (macros, actually) for
handling variable arguments declared with the '...' (ellipsis)
operator.  They are not described in the libc reference for DJGPP 2.01,
but you can find details and examples in the comp.lang.c FAQ
(http://www.eskimo.com/~scs/C-faq/top.html), chapter 15.

Broken down simply, you use the following basic syntax:

#include <stdarg.h>

int printf_clone( const char *fmt, ... );

int printf_clone( const char *fmt, ... )
{
    va_list v;
    int r;

    va_start(v, fmt);
    r = vprintf( fmt, v );
    va_end(v);
    return r;
}

The important parts are the va_start() and va_end() macros; what you do
inbetween is your business.  The va_arg() macro is called to get the
actual arguments--look at the source of vprintf(), vsprintf(), and
vfprint() to see a detailed example of usage--you pass it the va_list
variable and the type of the data you want to extract, for each piece of
data.  You must use your own methods to determine how many data items
were passed and of what type.  See chapter 15.10 of the comp.lang.c FAQ
for potential pitfalls.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I |     mailto:fighteer AT cs DOT com      |
|       ICQ UIN#:  7406319        |   http://www.cs.com/fighteer/   |
| Descent 2: The Infinite Abyss - The greatest Internet game of all |
| time just got better!  This time, you're going all the way down...|
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019