Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional arguments in C function

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)
{
    // write string here
    // write value here, if it exists
}

When used if(value != NULL) statement, my Console() function sends 4096

How can I check and act based on argument existence?

like image 236
Eray CANLI Avatar asked Jan 06 '15 09:01

Eray CANLI


People also ask

Does C have optional arguments?

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 ).

What is optional in a function declaration C?

A function has two parts: the specification and the body. The specification (spec for short) begins with the keyword FUNCTION and ends with the RETURN clause, which specifies the datatype of the return value. Parameter declarations are optional. Functions that take no parameters are written without parentheses.

How do you make an optional argument a function?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

What is an optional parameter in C #?

A method that contains optional parameters does not force to pass arguments at calling time. It means we call method without passing the arguments. The optional parameter contains a default value in function definition. If we do not pass optional argument value at calling time, the default value is used.


1 Answers

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 <stdarg.h> variadic facilities (which are often implemented as compiler builtins).

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).

like image 137
Basile Starynkevitch Avatar answered Sep 20 '22 13:09

Basile Starynkevitch