Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ellipsis operator in c [duplicate]

Possible Duplicate:
What's does … mean in an argument list in C ?

 function fun1(...)
    {
    }

Please tell me about what is the use and how to use ellipsis operator in c. Thanks,

like image 332
Karandeep Singh Avatar asked Sep 25 '10 06:09

Karandeep Singh


People also ask

What does 3 dots mean in C?

The three dots '...' are called an ellipsis. Using them in a function makes that function a variadic function. To use them in a function declaration means that the function will accept an arbitrary number of parameters after the ones already defined.

What is Va_list in C?

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.

What are Variadic functions in C?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

How many parameters are passed in Ellipse function?

It takes two parameters: the va_list and the last specified non-ellipsis parameter. To get each ellipsis parameter, the va_arg macro requires the va_list object ( list ) and the type of the last non-ellipsis parameter. Our code example contains only one specified parameter, num of type int.


1 Answers

An ellipsis is used to represent a variable number of parameters to a function. For example:

void format(const char* fmt, ...)

The above function in C could then be called with different types and numbers of parameters such as:

format("%d-%d-%d", 2010, 9, 25);

and

format("product: %s, price: %f", "HDD", 450.90);

C99 introduced Variadic macros which also makes use of ellipsis.

like image 82
Vijay Mathew Avatar answered Sep 20 '22 17:09

Vijay Mathew