passing variable arguments

Edward M. Markowski emm at iczer-1.UUCP
Sat Jun 9 09:34:10 AEST 1990


In article <353 at ankh.ftl.fl.us> chin at ankh.ftl.fl.us (Albert Chin) writes:
>How does printf() work. I believe there is some way in C to obtain the
>pointer to the argument list in a function. If so, printf() is pretty 
>simple. But how do you do this. Is it possible to get the number of
>arguments in a printf() statement?

The way I was learned to pass a varible number of arguments was to make
the first argument some value that could be counted.

Printf gets the address of the first argument( the format string ) it
scans it for % chars. When it finds a % char it uses the address of the
format string to find nhe next arg. The address of the format string
is a pointer to a location in the parameter stack. Printf just adds
( or subtracts ) the currect size from the pointer to the stack to get
the next parameter. Printf stops going up the stack when it runs out
of % chars in the format string, even if you did not pass it the correct
number of args.

The compiler does add any code to let the called function know how many
parameters there are for it on the stack. You must make shure your
function expects the same number of parameters as it was passed or
pass a variadle letting it know how many parameters there are.



-- 
Edward M. Markowski -- iczer-1 Administrator

                              ...the garage is flooded from the sprinkler.
                              It also left a man's decapitated body,
VOICE : (201) 478-6052        lying on the floor next to his own severed head.
UUCP : ..!uunet!iczer-1!emm   A head which at this time has no name.



More information about the Comp.lang.c mailing list