Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C : write a function doesn't know how many parameter

When learning C I see that printf can receive many arguments as it is passed.

And I don't know how C can implement a function like this, where the user can type as many parameters as the user wants. I have thought about pointers, too but still have no bright idea. If anyone has any ideas about this type of function, please tell me.

like image 771
hqt Avatar asked Dec 09 '22 23:12

hqt


2 Answers

You need to use va_args, va_list and the like. Have a look at this tutorial. http://www.cprogramming.com/tutorial/c/lesson17.html

That should be helpful.

like image 163
Cygnus Avatar answered Dec 18 '22 16:12

Cygnus


You have to use the ... notation in your function declaration as the last argument.

Please see this tutorial to learn more: http://www.cprogramming.com/tutorial/c/lesson17.html

like image 43
DPlusV Avatar answered Dec 18 '22 18:12

DPlusV