List of variable parameters of c Functions

Source: Internet
Author: User

List of variable parameters of c Functions

The C language allows the use of variable parameter lists. The commonly used printf function is a variable parameter function, and the C Standard Library provides stdarg. h provides us with support for this aspect; this header file provides some types and macros to support variable parameter lists, including types va_list, macros va_start, va_arg, and va_end;

Variable Function Parameter definition method:

# Include <stdarg. h> void func (int count ,...) {va_list ap; int ix, tmp; va_start (ap, a); for (ix = 0; ix <count; ++ ix) {tmp = va_arg (ap, int ); // process the param} va_end (ap );}

The first parameter count of a function is essential, that is, the variable parameter list function you define must have at least one parameter;

Va_list: It is generally defined as char *;

Va_start (va_list ap, paramN): Initialize the ap value, pointing to the first variable parameter.

Va_arg (va_list ap, TYPE): obtains the actual value from the ap based on the TYPE and moves the ap pointer.

Va_end: Generally, nothing is done.

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.