Variable parameters in C Language

Source: Internet
Author: User

Variable parameters in C Language

The function of variable parameters in C language is really very large. Since I published an article on how to implement printf, fprintf, and sprintf, many bloggers have asked me about the mechanism, I also explained the related knowledge points. Today, we take this opportunity to give an example to see how to use a variable parameter to accumulate and return data. Please refer to the Code:

# Include

 
  
# Include

  
   
Int add (int count ,...) {va_list ap; int I, sum; va_start (ap, count);/* Initialize the argument list. */sum = 0; // accumulate all integers in the passed parameter for (I = 0; I <count; I ++) sum + = va_arg (ap, int);/* Get the next argument value. */va_end (ap);/* Clean up. * // return the accumulated value return sum;} int main (void) {printf ("% d \ n", add (3, 5, 5, 6 )); printf ("% d \ n", add (10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); return 0 ;}

  

 
Running result:

We can see that after the input parameters are added, the result returned by printf is the result returned by the function.


 

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.