r/C_Programming • • 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.

91 Upvotes

39 comments sorted by

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.

19

u/Sophiiebabes Aug 28 '26

I thought unnamed parameters was a thing in C since the K&R days?

37

u/aioeu Aug 28 '26 edited Aug 28 '26

They could be omitted in function declarations since C89, but not function definitions.

(Well, except in the special case where the function had a single "unnamed parameter" of type void, of course. Syntactically that is treated as if it were a parameter, even though it doesn't act like a parameter at all, and until C23 it was the only case where the identifier could — indeed must — be omitted.)

3

u/Sophiiebabes Aug 28 '26

That's exactly what I was thinking of, thank you 🙏

1

u/javascript Aug 28 '26

I would call it two characters, because you need a whitespace right before it.

1

u/frayien Aug 29 '26

btw, C++26 will allow multiple redeclaration of the name "", and specifically not warn about unused variable named "". (As long as you effectively dont use it, if you do "_" will be treated like a normal name)

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.

16

u/beephod_zabblebrox Aug 28 '26

how often do you need a parameter that must not be used?

5

u/bothunter Aug 28 '26

Win32 API is a good example of this. You'll see plenty of functions that have parameters "reserved for future use"

It lets them evolve the APIs without breaking the contracts or making entirely new versions of the function.

1

u/OtherOtherDave Aug 28 '26

You can do the same thing (and IMHO more cleanly) by just having your API’s public function provide suitable default values to call an internal private function which has all the parameters. If the time ever comes to make those parameters part of the public API, just add that full function declaration to the relevant header and you’re done.

11

u/trolley813 Aug 28 '26

E.g. if you want to implement some interface which requires passing a parameter, but this parameter is useless/meaningless in your implementation.

7

u/beephod_zabblebrox Aug 28 '26

well if its useless in the implementation then you wouldn't use it anyway?

8

u/Wertbon1789 Aug 28 '26

E.g. you implement a callback, which typically give the option of passing in a userdata pointer, but in your implementation you don't use it, meaning dereferenceing it will be a segfault. Clearly expressing this in code needs more than just a missing name or a "unused" name or attribute, but it's part of it at least.

1

u/beephod_zabblebrox Aug 28 '26

but you wouldnt use the userdata pointer if nothing is there

i guess if there's a million different places where it is and you need to access the same data from elsewhere...

3

u/Wertbon1789 Aug 28 '26

Yes, but it's for denoting exactly that explicitly, or at least make it obvious. Sure, you know in the moment that you can't use it, but it's a hint for yourself or other people, including maybe a comment to spell it out explicitly.

3

u/glasket_ Aug 28 '26

It's for prevention/security, just like const. You could make very similar arguments for const not being useful because "you can just not write to the value," but const provides a way for you to make it explicit that you shouldn't write to the value. Unnamed parameters make it explicit that you shouldn't interact with that argument at all.

13

u/trolley813 Aug 28 '26

Yes, but you may use it accidentally, and the tools won't warn you since it's perfectly legitimate way.

1

u/Square-Singer Aug 29 '26

You are right that it's not necessary from a pure executed code standpoint, but it can be helpful for tooling/linting.

Imagine the following type of use case: You are implementing a function that accesses some file system. The interface is broad and tries to handle both HDDs and sold state media like SSDs or SD cards. Because of that it has "Cylinder Head Sector" data in the signature.

Your implementation is for SD-cards.

An SD will never have any cylinder or head values. The parameters will always contain garbage data.

By marking these parameters as "must not be used" you make sure that neither you nor anyone who will maybe end up working on that implementation in the future will ever have any ambiguity in regards to whether these parameters contain useful information.

If you or anyone else comes back to this implementation years later, it's instantly clear to you or them that these parameters are useless obsolete garbage and should not ever be used. Also, if you accidentally end up using these parameters, the compiler will tell you that you can't.

Especially in long-running projects and FOSS it's super helpful to have info like that directly encoded in the code, because it will prevent errors and mistakes before they happen.

1

u/lestofante Aug 28 '26

That sound like a tagged union with hidden contract (from the compiler point of view)

1

u/RRumpleTeazzer Aug 28 '26

interfaces, i.e. structs of function pointers.

for the interface, sure. but sometimes also for implementations.

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/RealisticDuck1957 Aug 29 '26

I find the latter more readable. But that is a subjective human judgement. The language doesn't care.

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/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

2

u/C_Programming-ModTeam Aug 28 '26

Rude or uncivil comments will be removed. If you disagree with a comment, disagree with the content of it, don't attack the person. Provide your reasoning, not just a judgement.

0

u/mikeblas Aug 28 '26

Can you find a way to make your point without being insulting?

0

u/[deleted] Aug 28 '26

[deleted] — view deleted comment

-1

u/arthurno1 Aug 28 '26

Well obviously other people didn't agree with you about my comment being insulting, which it wasn't. Taking on the mod hat to reinforce your opinion does not do much difference. But if it is moderated by people like you I can just leave the forum.