Solved

what are va_ macros in C ?

Posted on 2004-04-18
5
705 Views
Last Modified: 2007-12-19

Can anybody plz give me some information on the va_ macros in C  ?

Thanks !
0
Comment
Question by:paragm78
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
Mercantilum earned 25 total points
ID: 10852290
You use va_ macros when you need to provide a variable number of arguments to a function.
E.g. printf(...);
Please have a look at
http://www.cybertiggyr.com/gene/htdocs/portable/portable.html
http://www.eskimo.com/~scs/cclass/int/sx11b.html
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 25 total points
ID: 10852292
 va_arg, va_end, va_start       <STDARG.H>

 Macros that implement variable argument lists

 Declaration:
   void va_start(va_list ap, lastfix);
   type va_arg(va_list ap, type);
   void va_end(va_list ap);

 Remarks:
Some C functions, such as vfprintf and vprintf, take variable argument lists
in addition to taking a number of fixed (known) parameters.

The va_arg, va_end, and va_start macros provide a portable way to access
these argument lists.

 va_start sets ap to point to the first of the variable arguments being
passed to the function.

 va_arg expands to an expression that has the same type and value as the
next argument being passed (one of the variable arguments).

Because of default promotions, you can't use char, unsigned char, or float
types with va_arg.

 va_end helps the called function perform a normal return.

Argument ³ What It Is/Does

  ap       Points to the va_list variable argument list being passed to
             the function.
             The ap to va_arg should be the same ap that va_start
             initialized.
             NOTE: va_end might modify ap in such a way that it can't be
             used unless va_start is recalled.
           
  lastfix  The name of the last fixed parameter being passed to the
       called function.
  type      Used by va_arg to perform the dereference and to locate the
           following item.

You use these macros to step through a list of arguments when the called
function does not know the number and types of the arguments being passed.

 Calling Order

va_start must be used before the first call to va_arg or va_end.

va_end should be called after va_arg has read all the arguments. Failure to
do so might cause strange, undefined behavior in your program.

 Stepping Through the Argument List

The first time va_arg is used, it returns the first argument in the list.

Each successive time va_arg is used, it returns the next argument in the
list.

It does this by first dereferencing ap, then incrementing ap to point to the
next item.


 Return Value:
va_start and va_end return no values.

va_arg returns the current argument in the list (the one that ap is pointing
to).
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10852294
Here is an example:

#include <stdio.h>
#include <stdarg.h>

/* calculate sum of a 0 terminated list */
void sum(char *msg, ...)
{
   int total = 0;
   va_list ap;
   int arg;
   va_start(ap, msg);
   while ((arg = va_arg(ap,int)) != 0) {
      total += arg;
   }
   printf(msg, total);
   va_end(ap);
}

int main(void) {
   sum("The total of 1+2+3+4 is %d\n", 1,2,3,4,0);
   return 0;
}
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now