Solved

what are va_ macros in C ?

Posted on 2004-04-18
5
725 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 171
SQL handling single and double quotes 3 97
Resolve Dependency Issues 4 79
A  &&  B  || (C||D) how it works in c 7 101
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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

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