Solved

what are va_ macros in C ?

Posted on 2004-04-18
5
739 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
[X]
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
  • 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

Technology Partners: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

751 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