Usage and example of variable parameters in C Language
Source: Internet
Author: User
Usage and example of variable parameters in C language-Linux general technology-Linux programming and kernel information. The following is a detailed description. Syntax:
# Include
Type va_arg (va_list argptr, type );
Void va_end (va_list argptr );
Void va_start (va_list argptr, last_parm );
The va_arg () macros are used to pass a variable number of arguments to a function.
First, you must have a call to va_start () passing a valid va_list and the mandatory first argument of the function. this first argument can be anything; one way to use it is to have it be an integer describing the number of parameters being passed.
Next, you call va_arg () passing the va_list and the type of the argument to be returned. The return value of va_arg () is the current parameter.
Repeat callto va_arg () for however extends arguments you have.
Finally, a call to va_end () passing the va_list is necessary for proper cleanup.
For example:
Int sum (int num ,...){
Int answer = 0;
Va_list argptr;
Va_start (argptr, num );
For (; num> 0; num --){
Answer + = va_arg (argptr, int );
}
Va_end (argptr );
Return (answer );
}
Int main (void ){
Int answer = sum (4, 4, 3, 2, 1 );
Printf ("The answer is % d \ n", answer );
Return (0 );
}
This code displays 10, which is 4 + 3 + 2 + 1.
Here is another example of variable argument function, which is a simple printing function:
Void my_printf (char * format ,...){
Va_list argptr;
Va_start (argptr, format );
While (* format! = '\ 0 '){
// String
If (* format ='s '){
Char * s = va_arg (argptr, char *);
Printf ("Printing a string: % s \ n", s );
}
// Character
Else if (* format = 'C '){
Char c = (char) va_arg (argptr, int );
Printf ("Printing a character: % c \ n", c );
Break;
}
// Integer
Else if (* format = 'D '){
Int d = va_arg (argptr, int );
Printf ("Printing an integer: % d \ n", d );
}
* Format ++;
}
Va_end (argptr );
}
Int main (void ){
My_printf ("sdc", "This is a string", 29, 'x ');
Return (0 );
}
This code displays the following output when run:
Printing a string: This is a string
Printing an integer: 29
Printing a character: X
// One instance
Program requirements:
Create a LOG function
Log_printfbuf (char * fmt ,...)
Print the same content of printf, and then print the buffer content of the remaining two parameters.
The first parameter is buffer, and the second parameter is the number of bytes to be printed.
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