Number of arguments in a va_list ?

Posted on 2003-12-02
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 );


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

649 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