The variable parameter list is used to calculate the average value of n numbers. The number of parameter lists

Source: Internet
Author: User

The variable parameter list is used to calculate the average value of n numbers. The number of parameter lists

Va_list arg; declares a va_list variable arg, which is used to access the undetermined part of the parameter list;

Va_start (arg, val); the first parameter is the name of the va_list variable, and the second parameter is the first parameter of the uncertain parameter;

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

The name of the va_list variable. The second is the type of the uncertain 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 ); printf ("% f \ n", ret); return 0 ;}</span>


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.