r/C_Programming • u/Linguistic-mystic • Aug 28 '26
One of the coolest features of C: omitted parameter names
In all programming languages, there are places where you need to write a function with some parameters that are unused. Mostly to satisfy some callback signature or functional interface. But C makes it easiest of all languages. Where in other languages you have to give the compiler hints like @SuppressWarnings("unused") or name _: String to prevent it from warning you that this parameter here is unused, in C you can just omit the parameter name:
void
foo(int arg, char*) {
...implemenation
}
That's it, no hints, no warnings (even with -Wextra), no nuthin'. The compiler understands that if you haven't given this parameter a name, then you intend it to be unused. This is the most concise of all languages and you don't even have to come up with a (useless) name.
104
u/aioeu Aug 28 '26 edited Aug 28 '26
Well, C only got this in C23, many years after it being in C++, and I would be rather surprised if C++ invented it.
I think requiring
_is generally fine, so long as it actually acts like an "unnamed" parameter (e.g. you can have more than one_parameter). It's only one character, and there are benefits in having a consistent syntax.But yes, unnamed parameters can be useful.
28
u/questron64 Aug 28 '26
In C23 only, and I'm not really sure I would classify that as "coolest." It's a very, very minor feature. This feature does, however, fix a problem. In C there's no other way to express a parameter whose value must not be used. You can use compiler-specific attributes (though, to my knowledge none exactly fulfil that role), or the C23 [[maybe_unused]], but you can still use the parameter. I used to name unused parameters like unused_foo so it's at least obvious you shouldn't use it, but there was no way to outright disallow using it. But if it has no name then you can't use it.
7
u/PrimozDelux Aug 28 '26
Why wouldn't you want to name the parameter anyways? If I read your function I would like to know what the unused parameter is for in order to better understand what is happening.
10
u/Breath-Present Aug 28 '26
C++ has this since C++98 though, hardly unique to C
4
u/Unfair-Ocelot-2363 Aug 28 '26
"char*"? i prefer "char *"
3
u/ReallyEvilRob Aug 28 '26
Been programming in C for a long time and I've never actually seen this before.
5
u/SoggyStress7785 Aug 28 '26
If that's cool...
2
u/crouchingarmadillo Aug 28 '26
You could also just mark something explicitly as unused by doing a void cast.
2
u/flatfinger Aug 28 '26
A cooler feature of old versions of C was the ability to have optional parameters. If a function would sometimes use one of its later arguments and sometimes not, callers could omit the argument in cases where the called function wasn't going to use it. This would often improve code generation efficiency because there was no need for the caller to spend time setting the value of the passed parameter.
Unfortunately, there was no way to indicate when such usage should be considered incorrect and produce a diagnostic, and when prototypes were added they didn't include a means of supporting the old semantics. While stdarg.h would allow a function to process a function that would appear from the outside to use optional parameters, a function that uses stdarg.h will often need to use extra code internally to access any optional parameters, rather than simply accessing optional parameters using the same machine code that would be used to access any other.
1
u/TransientVoltage409 Aug 28 '26
Some compilers have features to mark an argument as unused (gcc's __attribute__ for one). But compiler dependency can be a problem.
You might suppress an unused-arg warning by sticking something like
foo = foo;somewhere in the function. I don't know if it's valid, I only remember doing it once years ago.if (foo == foo);might work for a const. I could be wrong.1
u/st_heron Aug 29 '26
I don't get the use case
1
u/Traveling-Techie Aug 29 '26
TIL…
1
u/pthierry Aug 29 '26
Does that mean that a function can add an argument to its signature and the code will still compile with zero warning despite all the call sites that are now using the previous API?
1
u/alatennaub Aug 29 '26
Interesting. Didn't know that about C. But there are other languages that use the same design (and quite actively in theor ecosystem). In Raku, you can just use a type as a parameter without a variable and it works the same. You can even just put in a anonymous var (
sub foo ($arg, $)) as an equivalent to using the void type in C in this place. That adds some visual distinctiveness to emphasize the "I don't care about this".1
u/septum-funk Aug 31 '26
prior to c23 this is done with `(void)pname;` lol not exactly elegant
0
u/[deleted] Aug 28 '26
[deleted] — view deleted comment