Function with variable number of args

at BRL-AOS.ARPA at BRL-AOS.ARPA
Tue Jun 26 13:03:41 AEST 1984


From:  Eric Benson <@BRL-AOS.ARPA:>

In BCPL, all values are the same size, one "word."  If a function is
called with six arguments, they are contained in exactly six words.
In C, some data types (e.g, doubles on VAXen, longs, floats and
doubles on 11's) do not fit in one word.  _doprnt has to increment its
argument pointer by different amounts depending on the data type,
which it determines by looking at the corresponding printf format.
Notice what happens when you try

printf("This is not an integer: %d, but these are floats: %f, %f and %f",
				1.0,			 2.0, 3.0,   4.0);

You expect garbage to be printed in the first case, but you may be
surprised to see garbage in the other cases as well.  That happens
because you have made the argument pointer point into the middle of
1.0.  The next thing to be printed is not 2.0, but part of 1.0 and
part of 2.0.

It is for this reason, among others, that it is difficult to define a
portable facility for handling a variable number of arguments.

-- Eric
-------



More information about the Comp.unix.wizards mailing list