Path: utzoo!utgpu!news-server.csri.toronto.edu!clyde.concordia.ca!uunet!iczer-1!emm From: emm@iczer-1.UUCP (Edward M. Markowski) Newsgroups: comp.lang.c Subject: Re: passing variable arguments Message-ID: <320@iczer-1.UUCP> Date: 8 Jun 90 23:34:10 GMT References: <353@ankh.ftl.fl.us> Reply-To: emm@iczer-1.UUCP (Edward M. Markowski) Organization: The Kurowulf Empire Lines: 32 In article <353@ankh.ftl.fl.us> chin@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.