C Standard Library Reference Guide series translations (10) stdarg. h

Source: Internet
Author: User

Http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.10.html.

Author: Eric Huss

Chinese translator: Liu jinhong poechant

Copyright Disclaimer: the original text in this article is copyrighted by Eric Huss, and the Chinese translation is copyrighted by poechant. Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant

10. stdarg. h

The stdarg header file defines the macro used to obtain parameters when the number of function parameters is located.

MACRO:

Va_start ();

Va_arg ();

Va_end ();

Type:

Typedef va_list

10.1.
Variables and definitions

Va_listApplies to the type of parameters that enter functions with stdarg macros.

A function with an uncertain number of parameters(,...)It is marked at the end of the parameter list.

10.2. va_start

Statement:

Void va_start (va_listAP,Last_arg);

Initialization andVa_argAndVa_endUsed in combination with macrosAP.Last_argIs the most deterministic parameter passed to the function (the parameter before the ellipsis ). Note:Va_startIn useVa_argAndVa_end.

10.3. va_arg

Statement:

TypeVa_arg (va_listAP,Type);

In the parameter list (that isAP) Followed by another parameter.Type. Note:APMust beVa_startInitialization. If no parameter is set, the result is undefined.

10.4. va_end

Statement:

Void va_end (va_listAP);

Allows a macro with parameters to be called.Va_start. IfVa_endIf the function is not called before return, the result is undefined. Parameter ListAPAfterVa_endAfter and not calledVa_startIt cannot be used before.

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 );

}

This series of translations is being updated continuously

(1) assert. HC standard Library Reference Guide series (2) ctype. HC standard Library Reference Guide series (3) errno. HC standard Library Reference Guide series (4) float. HC standard Library Reference Guide series (5) limits. HC standard Library Reference Guide series (6) locale. HC standard Library Reference Guide series (7) math. HC standard Library Reference Guide series (8) setjmp. HC standard Library Reference Guide series translations (9) signal. HC standard Library Reference Guide series translations (10) stdarg. HC standard Library Reference Guide series (11) stddef. HC standard Library Reference Guide series translations (12) stdio. H ()

Copyright Disclaimer: the original text in this article is copyrighted by Eric Huss, and the Chinese translation is copyrighted by poechant. Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant

-

Related Article

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.