📜  C 中的可变参数函数

📅  最后修改于: 2022-05-13 01:54:58.379000             🧑  作者: Mango

C 中的可变参数函数

可变参数函数是可以接受可变数量参数的函数。在 C 编程中,可变参数函数增加了程序的灵活性。它接受一个固定参数,然后可以传递任意数量的参数。可变参数函数由至少一个固定变量和作为最后一个参数的省略号 (...) 组成。

句法:

想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程
int function_name(data_type variable_name, ...);

可以通过名为的头文件访问传递参数的值:

#include 

包括以下方法:

Methods

Description

va_start(va_list ap, argN)This enables access to variadic function arguments.
va_arg(va_list ap, type)This one accesses the next variadic function argument.
va_copy(va_list dest, va_list src)This makes a copy of the variadic function arguments.
va_end(va_list ap)This ends the traversal of the variadic function arguments.

此处, va_list保存va_startva_argva_endva_copy所需的信息。

方案一:

以下简单的 C 程序将演示可变参数函数AddNumbers()的工作:

C
// C program for the above approach
 
#include 
#include 
 
// Variadic function to add numbers
int AddNumbers(int n, ...)
{
    int Sum = 0;
 
    // Declaring pointer to the
    // argument list
    va_list ptr;
 
    // Initializing argument to the
    // list pointer
    va_start(ptr, n);
 
    for (int i = 0; i < n; i++)
 
        // Accessing current variable
        // and pointing to next one
        Sum += va_arg(ptr, int);
 
    // Ending argument list traversal
    va_end(ptr);
 
    return Sum;
}
 
// Driver Code
int main()
{
    printf("\n\n Variadic functions: \n");
 
    // Variable number of arguments
    printf("\n 1 + 2 = %d ",
           AddNumbers(2, 1, 2));
 
    printf("\n 3 + 4 + 5 = %d ",
           AddNumbers(3, 3, 4, 5));
 
    printf("\n 6 + 7 + 8 + 9 = %d ",
           AddNumbers(4, 6, 7, 8, 9));
 
    printf("\n");
 
    return 0;
}


C
// C program for the above approach
#include 
#include 
 
// Variadic function to add numbers
int LargestNumber(int n, ...)
{
    int min = 0;
 
    // Declaring pointer to the
    // argument list
    va_list ptr;
 
    // Initializing argument to the
    // list pointer
    va_start(ptr, n);
 
    for (int i = 0; i < n; i++) {
 
        // Accessing current variable
        // and pointing to next
        int temp = va_arg(ptr, int);
        min = temp > min ? temp : min;
    }
 
    // End of argument list traversal
    va_end(ptr);
 
    return min;
}
 
// Driver Code
int main()
{
    printf("\n\n Variadic functions: \n");
 
    // Variable number of arguments
    printf("\n %d ",
           LargestNumber(2, 1, 2));
 
    printf("\n %d ",
           LargestNumber(3, 3, 4, 5));
 
    printf("\n %d ",
           LargestNumber(4, 6, 7, 8, 9));
 
    printf("\n");
 
    return 0;
}


输出:
Variadic functions: 

 1 + 2 = 3 
 3 + 4 + 5 = 12 
 6 + 7 + 8 + 9 = 30

程序 2:下面是由可变参数函数LargestNumber()组成的 C 程序:

C

// C program for the above approach
#include 
#include 
 
// Variadic function to add numbers
int LargestNumber(int n, ...)
{
    int min = 0;
 
    // Declaring pointer to the
    // argument list
    va_list ptr;
 
    // Initializing argument to the
    // list pointer
    va_start(ptr, n);
 
    for (int i = 0; i < n; i++) {
 
        // Accessing current variable
        // and pointing to next
        int temp = va_arg(ptr, int);
        min = temp > min ? temp : min;
    }
 
    // End of argument list traversal
    va_end(ptr);
 
    return min;
}
 
// Driver Code
int main()
{
    printf("\n\n Variadic functions: \n");
 
    // Variable number of arguments
    printf("\n %d ",
           LargestNumber(2, 1, 2));
 
    printf("\n %d ",
           LargestNumber(3, 3, 4, 5));
 
    printf("\n %d ",
           LargestNumber(4, 6, 7, 8, 9));
 
    printf("\n");
 
    return 0;
}
输出:
Variadic functions: 

 2 
 5 
 9