Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C support optional null parameters?

In Python, I'm used to things like

def send_command(command, modifier = None):

and then the modifier argument is optional, and the absence of the argument can be differentiated from an argument of 0. Is there similar functionality in C? I'm inexperienced with C, and Googling, but can't find a clear statement of how to use optional parameters in C. It seems you can assign them similarly, like this:

void send_command(uint8_t command, uint8_t modifier = 0) {

so the second argument is optional and defaults to 0 if not used? (Edit: No, this is invalid C anyway)

But can the function distinguish between send_command(SOMETHING) and send_command(SOMETHING, 0)? Ideally, the second parameter could be any uint8 value, including 0.

Maybe NULL is different from 0?

void send_command(uint8_t command, uint8_t modifier = NULL) {
like image 781
endolith Avatar asked Feb 07 '12 16:02

endolith


People also ask

Does C support optional parameters?

The C Programming Language has no optional parameters.

Can you pass NULL as a parameter in C?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter.

How do you pass an optional parameter in C++?

You don't pass option parameters. You pass optional arguments! For more explicit control than that provided by reserving sentinel values, check out boost::optional<>.

What an optional parameter is in C #?

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

C does not support optional parameters. Nor does it support function overloading which can often be used to similar effect.

like image 90
David Heffernan Avatar answered Sep 28 '22 10:09

David Heffernan