Variable parameter in C

Sometimes in some cases, you want to have a function that takes parameters instead of predefined parameters. The C language provides you with a solution to this situation and you are allowed to define a function that accepts function parameters based on your requirements. Here is an example for defining such functions:

 int tenham ( int , . ) { . . . } int main () { tenham ( 1 , 2 , 3 ); tenham ( 1 , 2 , 3 , 4 ); } 

You should keep in mind that the tenham () function has a static final parameter, for example: three dots (.) and the preceding parameter is always an int that represents the total number of variable parameters passed. To use this feature, you need to use the file Header, stdarg.h, which provides functions and macros to perform this feature in the following steps:

Defining a function with the last parameter in static form and the parameter before it is always an int that represents the number of parameters.

Create a variable type va_list in the function definition. This type is defined in stdarg.h.

Use the int parameter and macro as va_start to initialize the va_list variable to a parameter list. This macro and_start is defined in stdarg.h.

Use macros as va_arg and va_list to access each item in the parameter list.

Use a macro as the Va_end to clear the specified memory to the Va_list variable.

Now follow the steps above and write a simple function that can take parameters and return their average values:

 #include #include double giatriTB ( int sothamso ,.) { va_list thamso ; double tong = 0.0 ; int i ; /* khoi tao thamso cho sothamso (la so tham so) */ va_start ( thamso , sothamso ); /* truy cap tat ca tham so da duoc gan cho thamso */ for ( i = 0 ; i < sothamso ; i ++) { tong += va_arg ( thamso , int ); } /* xoa bo nho danh rieng cho thamso */ va_end ( thamso ); return tong / sothamso ; } int main () { printf ( "Gia tri trung binh cua 7, 8, 9, 10 la: %fn" , giatriTB ( 4 , 7 , 8 , 9 , 10 )); printf ( "Gia tri trung binh cua 11, 22, 33 la: %fn" , giatriTB ( 3 , 11 , 22 , 33 )); printf ( "n===========================n" ); printf ( "QTM chuc cac ban hoc tot! n" ); } 

Compiling and running the above C program will produce the following results. You should keep in mind that the giatriTB () function is called twice and each time the first parameter represents the sum of the passed variable parameters. Only static parameters will be used to pass the number of parameters.

Variable parameter in C Picture 1

According to Tutorialspoint

Last lesson: Recursive in C

Next lesson: Memory management in C

5 ★ | 1 Vote

May be interested

  • How to set and list environment variables in LinuxHow to set and list environment variables in Linux
    if you want to do something that requires using the command line, you will still need to know about environment variables. this may seem like a complicated term, but actually environment variables are easy to understand.
  • How to fix 'The Parameter Is Incorrect' error in Windows 10How to fix 'The Parameter Is Incorrect' error in Windows 10
    sometimes while connecting an external hard drive, sd card, usb or other storage media to your windows pc, you may experience the parameter is incorrect error.
  • Call the function by pointer in C ++Call the function by pointer in C ++
    the method of calling a function by pointer in c ++ passes parameters to a function, copying the addresses of a parameter into the official parameter. inside the function, this address is used to access the actual parameter used in the function call. that is, changes made to the official parameter affect the passed parameter.
  • Data type in C programmingData type in C programming
    in the c programming language, data types refer to the system extension used for variable declarations of different types. the type of variable determines the amount of memory used to store that variable and how the bits are stored when released
  • Variable in JavaScriptVariable in JavaScript
    one of the most distinctive features of a program language is the set of data types it supports. this is the type of value that can be represented and manipulated in the program language.
  • Scope rules in programming CScope rules in programming C
    a scope in any program is an area in the program where a defined variable exists and outside of that range the variable cannot be accessed.
  • Difference between arguments and parameters in C/C++Difference between arguments and parameters in C/C++
    an argument is a reference to the values ​​passed into a function when the function is called.
  • How to Code an Alert with a Variable Using JavascriptHow to Code an Alert with a Variable Using Javascript
    alerts with variables are among the most useful things in javascript coding. you can use them to refer to someone by their name, to make a mini madlibs game, or even for showing quiz results. this article will show you how to make a...
  • Call the function by value in C ++Call the function by value in C ++
    the method calls the function by the value of the parameters passed to a function that copies the actual value of a parameter into the official argument of the function. in this case, changes made to the parameter inside the function have no effect on the parameter.
  • How to Use Excel VBA Variable Data TypesHow to Use Excel VBA Variable Data Types
    you will learn to create the different types of excel vba (visual basic for applications) variable data types. vba makes life easy for programmers because it can handle all the details involved in dealing with data automatically. for...