Using variable parameter list to achieve average of n number

Source: Internet
Author: User

Va_list Arg; Declares a variable arg of type va_list, which is used to access the indeterminate part of the parameter list;

Va_start (Arg,val); The first parameter is the variable name of Va_list, and the second parameter is the previous parameter of the indeterminate parameter;

A_arg (arg,int); returns the value of this parameter and uses var_arg to point to the next mutable parameter, the first parameter is

The variable name of the Va_list, and the second is the type of the indeterminate parameter

<span style= "FONT-SIZE:24PX;" > #include <stdio.h> #include <stdarg.h>double average (int val,...) {va_list arg;double Sum=0.0;va_start (arg,val); int i=0;for (i=0;i<val;i++) {sum+=va_arg (arg,int);} Va_end (ARG); return sum/val;} int main () {double ret=average (5,1,2,3,4,5);p rintf ("%f\n", ret); return 0;} </span>


Using variable parameter list to achieve average of n number

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.