Number of arguments in a va_list ?

Posted on 2003-12-02
Last Modified: 2008-02-01
How can I get the count of arguments in va_list ?

I want to program my own printf-function. So I want to know count of args or end of arg-list in myprintf(const char _FAR *__format, ...) - function.

Question by:bandolero
LVL 17

Expert Comment

ID: 9858658
I think you can't. printf() uses the format to figure out how many arguments ought to be there and hopes that they are.
LVL 10

Expert Comment

ID: 9858784
You will have to traverse thru the arguments to know the count

Help for all the required macros

Data Type: va_list

The type va_list is used for argument pointer variables.

Macro: void va_start (va_list ap, last-required)

This macro initializes the argument pointer variable ap to point to the first of the optional arguments of the current function; last-required must be the last required argument to the function.

See Old Varargs, for an alternate definition of va_start found in the header file varargs.h.

Macro: type va_arg (va_list ap, type)

The va_arg macro returns the value of the next optional argument, and modifies the value of ap to point to the subsequent argument.  Thus, successive uses of va_arg return successive optional arguments.

The type of the value returned by va_arg is type as specified in the call.  type must be a self-promoting type (not char or short int or float) that matches the type of the actual argument.

Macro: void va_end (va_list ap)

This ends the use of ap.  After a va_end call, further va_arg calls with the same ap may not work.  You should invoke va_end before returning from the function in which va_start was invoked with the same ap argument.

In the GNU C library, va_end does nothing, and you need not ever use it except for reasons of portability.

Sometimes it is necessary to parse the list of parameters more than once or one wants to remember a certain position in the parameter list.  To do this, one will have to make a copy of the current value of the argument.  But va_list is an opaque type and one cannot necessarily assign the value of one variable of type va_list to another variable of the same type.

Macro: void __va_copy (va_list dest, va_list src)

The __va_copy macro allows copying of objects of type va_list even if this is not an integral type.  The argument pointer in dest is initialized to point to the same argument as the pointer in src.

This macro is a GNU extension but it will hopefully also be available in the next update of the ISO C standard.

If you want to use __va_copy you should always be prepared for the possibility that this macro will not be available.  On architectures where a simple assignment is invalid, hopefully __va_copy will be available, so one should always write something like this:

  va_list ap, save;
#ifdef __va_copy
  __va_copy (save, ap);
  save = ap;



Expert Comment

ID: 9859882
I don't think there is a way to know the number of arguments of va_list.
Generally speaking it's a pointer to the current stack position and how many arguments were pushed to the stack depends on the caller code
Use vprintf instead
int vprintf( const char *format, va_list argptr );

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

LVL 49

Expert Comment

ID: 9860784
You cannot know the number of arguments passed unless the caller cooperates.  

Typically (as in printf), the function can look for something in the *first* argument (such as the number of %s and %d codings in the format string) or you can require that the first parameter be a count of the number of parameters that follow.    Or you can require that the *last*  argument have a special value (such as 0 or -1).

That is why varargs functions are considred to be "unsafe"  -- you can only *hope* that the caller followed the conventions... you cannot enforce it.   And a small programming mistake can be hard to locate -- catastophic errors typically appear only in certain conditions, based sometimes upon validity of user input.

-- Dan

Accepted Solution

colmcc earned 125 total points
ID: 9863967
In principle, it's quite easy to implement your own version of printf and related functions.  (There's even a example in K&R I think.)

If you just want to get hold of the normal output that printf would have produced, in order to display it in a window for example, then it's almost trivial -

int myprintf(char * fmt, ...)
 int nChars;
 char szBuf[BUFSIZE];
 va_list args;


 nChars = vsprintf(szBuf,args);

 // Do something with szBuf here.


 return (nChars);
It's a lot more complicated if you want to implement your own format specifiers like %s etc.  Unless there is a way of inheriting the standard printf behaviour and just adding your own new ones, then you have to implement it all from scratch.  I believe the example in K&R does this for just a few of the possible specifiers, but does at least demonstate a technique.


Expert Comment

ID: 9864263
  There is no Direct way to get the number of arguements of a va_list...... either you have to pass the number of arguements or you have to device a method to get the number of arguements from the first aguement.....

I think this topic has been already been covered......
check the following questions.......

LVL 49

Expert Comment

ID: 9864979
Oh!  Oh!  I want to change my answer!

LVL 17

Expert Comment

ID: 9865610
Take a slug from that Pan Galactic Gargle Blaster :-)

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question