Hello All
Is there a way to ensure that one of the function argument is set as
optional. So that it is not mandatory for the caller to pass
that parameter and yet if necessary can be used?
Short answer: no, there is no way to ensure that one formal argument is
optional.
Counter answer: if your function is defined as a variadic function (the last
parameter is an ellipsis), then C provides a logic mechanism to access
those parameters passed (or not) in the place of the ellipsis:
ISO/IEC 9899:1999 (E) ©ISO/IEC
7.15 Variable arguments <stdarg.h>
1 The header <stdarg.h> declares a type and deï¬nes four macros, for
advancing through a list of arguments whose number and types are not
known to the called function when it is translated.
2 A function may be called with a variable number of arguments of varying
types. As described in 6.9.1, its parameter list contains one or more
parameters. The rightmost parameter plays a special role in the access
mechanism, and will be designated parmN in this description.
3 The type declared is
va_list
which is an object type suitable for holding information needed by the
macros va_start, va_arg, va_end, and va_copy. If access to the varying
arguments is desired, the called function shall declare an object
(referred to as ap in this subclause) having type va_list. The object ap
may be passed as an argument to another function; if that function
invokes the va_arg macro with parameter ap, the value of ap in the
calling function is indeterminate and shall be passed to the va_end
macro prior to any further reference to ap.212)
7.15.1 Variable argument list access macros
1 The va_start, va_arg, and va_copy macros described in this subclause
shall be implemented as macros, not functions. It is unspeciï¬ed whether
va_end is a macro or an identiï¬er declared with external linkage. If a
macro deï¬nition is suppressed in order to access an actual function, or
a program deï¬nes an external identiï¬er with the name va_end, the
behavior is undeï¬ned. Each invocation of the va_start or va_copy macros
shall be matched by a corresponding invocation of the va_end macro in
the same function.
...