Variable parameter in C

Sometimes in some cases, when you want a function, you can get 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.

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.

Picture 1 of Variable parameter in C

According to Tutorialspoint

Last lesson: Recursive in C

Next lesson: Memory management in C

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile