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.
According to Tutorialspoint
Last lesson: Recursive in C
Next lesson: Memory management in C
You should read it
- Call the function by value in C ++
- Function in programming C
- Bool () function in Python
- Call the function by pointer in C ++
- Bin () function in Python
- Variable in C programming
- Memset () function in C
- The ord () function in Python
- The function printf () in C
- Int () function in Python
- PHP functions
- The strchr () function in C
Maybe you are interested
Discover the 7 eyes in Naruto and Boruto
What do the icons on the Android status bar mean?
Latest Dragon Pow Dragon Trainer Code and how to redeem code
Full code Jujutsu Infinite Roblox latest and how to redeem code
Microsoft Lists Why TPM, Secure Boot Are Mandatory on Windows 11
How to fix the error of not being able to copy and paste in Excel, Word