Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default parameters in C

Is it possible to set values for default parameters in C? For example:

void display(int a, int b=10){ //do something }  main(){   display(1);   display(1,2); // override default value } 

Visual Studio 2008, complaints that there is a syntax error in -void display(int a, int b=10). If this is not legal in C, whats the alternative? Please let me know. Thanks.

like image 788
user1128265 Avatar asked Feb 07 '12 23:02

user1128265


People also ask

Which is the default parameter?

Default parameter in Javascript The default parameter is a way to set default values for function parameters a value is no passed in (ie. it is undefined ). In a function, Ii a parameter is not provided, then its value becomes undefined . In this case, the default value that we specify is applied by the compiler.

What is the default parameters function?

Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.

What type of functions are default in C?

By default, C uses call by value to pass arguments.

What are parameters in C?

Parameters and Arguments Information can be passed to functions as a parameter. Parameters act as variables inside the function. Parameters are specified after the function name, inside the parentheses.


2 Answers

Default parameters is a C++ feature.

C has no default parameters.

like image 132
ouah Avatar answered Sep 23 '22 01:09

ouah


It is not possible in standard C. One alternative is to encode the parameters into the function name, like e.g.

void display(int a){     display_with_b(a, 10); }  void display_with_b(int a, int b){     //do something } 
like image 35
Joni Avatar answered Sep 23 '22 01:09

Joni