What does ... mean in an argument list in C?

I came across the following function signature and I wondered if this (the ellipsis, or "...") is some kind of polymorphism?

#include <fcntl.h>
int fcntl(int fd, int cmd, ... );

Thanks in advance.


It's a variable argument list.


That is a variadic function. See stdarg.h for more details.


The ... means that you can pass any number of arguments to this function, as other commenters have already mentioned. Since the optional arguments are not typed, the compiler cannot check the types and you can technically pass in any argument of any type.

So does this mean you can use this to implement some kind of polymorphic function? (I.e., a function that performs some operation based on the type of its arguments.)

No.

The reason you cannot do this, is because you cannot at runtime inspect the types of the arguments passed in. The function reading in the variable argument list is expected to already know the types of the optional arguments it is going to receive.

In case of a function that really is supposed to be able to take any number of arguments of any type (i.e., printf), the types of the arguments are passed in via the format string. This means that the caller has to specify the types it is going to pass in at every invocation, removing the benefit of polymorphic functions (that the caller doesn't have to know the types either).

Compare:

// Ideal invocation
x = multiply(number_a, number_b)
y = multiply(matrix_a, matrix_b)

// Standard C invocation
x = multiply_number(number_a, number_b)
y = multiply_matrix(matrix_a, matrix_b)

// Simulated "polymorphism" with varargs
x = multiply(T_NUMBER, number_a, number_b)
y = multiply(T_MATRIX, matrix_a, matrix_b)

You have to specify the type before the varargs function can do the right thing, so this gains you nothing.


No, that's the "ellipsis" you're seeing there, assuming you're referring to the ... part of the declaration.

Basically it says that this function takes an unknown number of arguments after the first two that are specified there.

The function has to be written in such a way that it knows what to expect, otherwise strange results will ensue.

For other functions that support this, look at the printf function and its variants.