[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1123
  • Last Modified:

Number of arguments in a va_list ?

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.

1 Solution
I think you can't. printf() uses the format to figure out how many arguments ought to be there and hopes that they are.
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;


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 );

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

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

Oh!  Oh!  I want to change my answer!

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now