Getting number of arguments from a "C"/C++ function (like printf).

Edouard Lagache lagache at picsou.inria.fr
Thu Jul 5 19:17:41 AEST 1990


   Hello "C" hackers of the world!

   A while back I sent out a question on how to figure out the number of
   arguments in a function that can take a variable number of arguments.  I
   got a lot of replies (Thanks to everyone who did reply!!!)  Fortunately
   for me they fall into two main groups:

   KISS-I (Keep It Simple Stupid!):
   Simply define the first argument to be the number of other arguments.
   Assuming the programmer using the function can count (even with math and
   reading scores the way it is . . .), it shouldn't be a problem, and that
   produces good efficient code.  However, that isn't quite as bullet proof as
   is theoretically desirable.  Still trusting a "C" programmer isn't as bad
   as trusting a user to do the right thing.  After all, "C" programmers are
   people like us (do you thrust yourself? . . . No!)

   KISS-II:
   Use the last argument of a sentinel.  That's okay too except
   that whatever argument you use better not ever be a real argument.
   Also that would not have solved my problem, since I needed the number
   of arguments.  To have done what I wanted, I would have needed to run
   through the list twice which I wasn't sure I could do.

   ( Now why did I come with this stuff? )

   Conjecture or fact?
   Now for the interesting part.  At least two people told me that the ANSI
   standard guarantees that I can run through the argument list twice.  To
   quote Stephen Clamage (message of Mon Jul  2 17:49:40 1990):

> If you have an ANSI-compatible implementation of C, handling of variable
> argument lists is completely portable, including scanning the list more
> than once.

   I have no doubt that the standard says that, I'm curious to know how many
   implementations actually support it.  Steve sent me some test code that
   I'll be glad to distribute to anyone wanting to explore this issue.  Indeed
   if people report their results, I'll tally up a list of the compilers that
   pass and fail this interesting test of ANSI compatibility.

   Happy "C" hacking!!

   Edouard Lagache
   INRIA, Sophia Antipolis - FRANCE. (for the summer)

   P.S. What about our "C" compiler?  We don't even have a 'stdargs.h' file!

   P.P.S. Thanks for all the nice greetings from all over the world! (It's a
          small world after all! :-)



More information about the Comp.lang.c mailing list