C Standard Library Reference Guide (stdarg.h)

Source: Internet
Author: User

Stdarg.h

The Stdarg header file defines a macro that is used to get arguments when the function argument is in the number position.

Macro:

Va_start ();

Va_arg ();

Va_end ();

Type:

typedef va_list

10.1. Variables and definitions

Va_list applies to the type of parameters that enter a function with Stdarg macros.

A function with an indeterminate number of parameters, used (,...) Marked at the end of the argument list.

10.2. Va_start

Statement:

void Va_start (Va_list ap, Last_arg);

Initializes an AP that is used in conjunction with the VA_ARG and Va_end macros. Last_arg is the most-determined parameter (which argument precedes the ellipsis) that is passed to the function. Note that Va_start is invoked before using Va_arg and Va_end.

10.3. Va_arg

Statement:

Type Va_arg (va_list ap, type);

A situation that follows another parameter type after the argument list (that is, the AP). Note that the AP must be initialized by Va_start. If there is no next argument, the result is undefined.

10.4. Va_end

Statement:

void Va_end (va_list ap);

Allows a function with a parameter to call the macro va_start to return. If Va_end is not called before the function returns, the result is undefined. The argument list AP is not allowed to be used until Va_end is invoked and Va_start is not invoked.

Instance:

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

void sum (char *, int, ...);   


int main (void)   
       
{   

  sum ("The sum of 10+15+13 is%d.\n", 3,10,15,13);   

  return 0;   

}   


void sum (char *string, int num_args, ...)   

{   

  int sum=0;   

  Va_list ap;   

  int loop;   


  Va_start (Ap,num_args);   

  for (loop=0;loop<num_args;loop++)   

    sum+=va_arg (ap,int);   


  printf (string,sum); 

  Va_end (AP);   

}

Original English: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.10.html

Original Author: Eric Huss

Chinese translator: Liu Yong poechant

Copyright: The original copy of this article is owned by Eric Huss, and the Chinese translation is poechant All rights reserved. Reprint please specify from "Liuda csdn blog": http://blog.csdn.net/poechant

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.