In a C function, I want to check if an input argument (\'value\' in my case) is presented or not.
i.e.:
void Console(char string[], int32_t value)
{
Optional arguments are generally not allowed in C (but they exist in C++ and in Ocaml, etc...). The only exception is variadic functions (like printf
).
Historically, the open(2) function from POSIX accepted in some cases an optional third argument (at the time it was defined - in the 1970s and 1980s -, the calling conventions practically pushed arguments on the call stack, so ignoring that argument was simple to implement). If you look today at recent implementation of that open
function in free software libc implementations on Linux, such as musl-libc, you see in its src/fcntl/open.c that it uses the
BTW, you could define some macros to fill the "missing" arguments, so if you have
void console(const char*, int32_t);
you might also
#define console_plain(Msg) console((Msg),0)
and that could be instead some inline function in some header, e.g.
static void inline console_plain (const char*msg)
{ console(msg, 0); }
then use console_plain("hello here")
elsewhere
Then your variadic function should define how and what arguments are allowed (after a non-empty sequence of fixed arguments). And use stdarg(3) to get these variadic (actual) arguments.
The actual arguments are known mostly at compile-time, not at run-time. So you need a convention which often defines which variadic argument are permitted from the required fixed arguments. In particular, you have no way to test that an argument is present (that information is lost at runtime).
BTW, with variadic functions you generally lose the typechecking that most C compilers provide (at least when you enable all warnings, e.g. gcc -Wall -Wextra
). If using GCC
you might have some function __attribute__-s (like format
, sentinel
, ....) in the prototype to assist that. You could even customize gcc
with the obsolete MELT, or in 2019 with your GCC plugin, to add your own attributes doing their own type checking.
How can I check and act based on argument existence?
With current usual calling conventions (e.g. study the x86-64 ABI) you generally cannot do that (without using variadic functions).