Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unnamed parameters in C

Tags:

c

gcc

macros

In C, unlike C++, all parameters to a function definition must be named.

Instead of quashing "unused parameter" errors with (void)a, or openly using __attribute__((unused)), I've created the following macro:

#define UNUSED2(var, uniq) UNUSED_ ## line ## var __attribute((unused))
// squash unused variable warnings, can it be done without var?
#define UNUSED(var) UNUSED2(var, __func__)

Used like this

void blah(char const *UNUSED(path)) {}

Is there some way I can guarantee a unique "dummy" variable name (obviously LINE and __func__ can't cut it), or neglect to name the unused variables at all?

Update0

The final code used is available here.

#ifdef __cplusplus
    // C++ allows you to omit parameter names if they're unused
#   define OMIT_PARAM
#else
    // A variable name must be provided in C, so make one up and mark it unused
#   define OMIT_PARAM3(uniq) const omitted_parameter_##uniq VARATTR_UNUSED
#   define OMIT_PARAM2(uniq) OMIT_PARAM3(uniq)
#   define OMIT_PARAM OMIT_PARAM2(__COUNTER__)
#endif

#ifdef _MSC_VER
#   define VARATTR_UNUSED
#else
#   define VARATTR_UNUSED __attribute__((unused))
#endif

It's used like this:

void blah(char const *OMIT_PARAM) {}

And avoids both unused parameter, unnamed parameter warnings, and guarantees that it's not going to clobber some other variable name.

like image 446
Matt Joiner Avatar asked Aug 15 '10 11:08

Matt Joiner


People also ask

What are different types of parameters in C?

In C, there are two types of parameters and they are as follows... The actual parameters are the parameters that are speficified in calling function. The formal parameters are the parameters that are declared at called function.

Can a function have no parameters in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

What does no parameters mean?

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function does not take any and should be defined with an empty set of parenthesis or with the keyword void. If no variable type is in front of a variable in the paramater list, then int is assumed.

How many parameters are there in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.


1 Answers

Stop looking for ugly non-portable, compiler-specific hacks. Even if the function does not use one of its arguments, presumably there's a reason the argument exists: to match a particular prototype/signature, most likely for the purpose of function pointer type compatibility. Assuming this is the case, the argument has a name determined by what the caller is expected to pass; the fact that your particular function is not using the argument for anything is largely irrelevant. So give it its proper name, and use __attribute__((unused)) if you insist on enabling this warning. I just always disable the unused argument warning because it's obviously bogus.

like image 64
R.. GitHub STOP HELPING ICE Avatar answered Sep 27 '22 02:09

R.. GitHub STOP HELPING ICE