On Thu, 18 Feb 1999, Anubhav Hanjura wrote:

# I was just wondering, how is the case of variable arguments eg. in printf/scanf etc. 
implemented in linux? 

here's the mail i got when i posted a similar thing to this list...

-- 
+++           Beware of programmers who carry screwdrivers           +++
[EMAIL PROTECTED]     http://www.users.globalnet.co.uk/~kermit
Here's what i got sent when i asked the question...

---------- Forwarded message ----------
Date: Wed, 9 Sep 1998 22:27:03 +0100 (BST)

[i keep my mails :) ]

From: Glynn Clements <[EMAIL PROTECTED]>
To: James <[EMAIL PROTECTED]>
Cc: Linux C Programming List <[EMAIL PROTECTED]>
Subject: Re: function parameters


James wrote:

> how do i create a function that has an undetermined number of parameters?

By using an ellipsis (`...') in the declaration and using the
va_{start,arg,end} macros (defined in stdarg.h) to access the
parameters.

> the definition for it has '...' as a parameter... what's that mean?

It means `plus any number of extra parameters'.

> and if you don't know how many parameters are being passed into the
> function beforehand, how do you know what the variable names are?

You don't. You need to use the va_* macros to access the additional
parameters.

You also need to be able to figure out how many parameters should have
been passed, and what their types are. printf() determines this from
the format string. Other possibilities include having a count
parameter and using a NULL argument to terminate the list (as is the
case for execl() and friends, and the XtVa* functions).

For example:

        #include <stdarg.h>
        #include <stdio.h>

        void contrived(int count, ...)
        {
                va_list va;
                int i;

                va_start(va, count);

                for (i = 0; i < count; i++)
                        printf("%s\n", va_arg(va, char *));

                va_end(va);
        }

        int main(void)
        {
                contrived(3, "hello", "there", "world");
                return 0;
        }

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to