Basic usage of C va macros

Source: Internet
Author: User

General steps for using VA macros:

Vsptr (Char * Format,...) // Remember the format here

{

Va_list argptr;

Va_start (argptr, format); // enables argptr to point to a bucket starting with format

Va_arg (argptr, type); // returns the current parameter of the parameter list pointed to by argptr, and causes argptr to point to the next parameter in the parameter list

...

Va_end (argptr); // stops, releasing the memory space occupied by argptr
}

 

To determine whether the parameters pointed to by argptr have been obtained:1-Number of items that can be passed2-Use functions of the vsprintf/vsnprintf series (all parameters can be removed once.End), The common usage of this function is as follows:

 

 

 

 

 

# Include <stdio. h>
# Include <stdarg. h>

# Define bufsize 80
Char buffer [bufsize];

Int vspf (Char * FMT,...)
{
Va_list argptr;
Int CNT;
Va_start (argptr, FMT );

CNT = vsnprintf (buffer, bufsize, FMT, argptr );

Va_end (argptr );

Return (CNT );
}

Int main (void)
{
Int inumber = 30;

Float fnumber = 90.0;

Char string [4] = "ABC ";
 
Vspf("% D % F % s ",Inumber, fnumber, string );
 
Printf ("% s/n", buffer );

Return 0;
}

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.