Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is f(void) deprecated in modern C and C++? [duplicate]

I'm currently refactoring/tidying up some old C code used in a C++ project, and regularly see functions such as:

int f(void) 

which I would tend to write as:

int f() 

Is there any reason not to replace (void) with () throughout the codebase in order to improve consistency, or is there a subtle difference between the two that I am unaware of?

More specifically, if a virtual member function in C++ is described as:

virtual int f(void) 

and a derived class includes a member function:

int f() 

is this a valid override? Additionally, am I likely to encounter any linker problems based on almost identical signatures?

like image 497
SmacL Avatar asked Jan 06 '09 12:01

SmacL


People also ask

Is void necessary in C?

If you omit the void , you have to search for function() and will therefore also find all function calls, making it more difficult to find the actual implementation. Show activity on this post. In C++, there is no difference in main() and main(void) . But in C, main() will be called with any number of parameters.

Can you have a void function in C?

Functions may be return type functions and non-return type functions. The non-return type functions do not return any value to the calling function; the type of such functions is void.

What is void f ()?

Void functions are created and used just like value-returning functions except they do not return a value after the function executes. In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value.

What is the difference between void foo void and void foo ()?

void foo() means "a function foo taking an unspecified number of arguments of unspecified type" void foo(void) means "a function foo taking no arguments"


1 Answers

In C, the declaration int f(void) means a function returning int that takes no parameters. The declaration int f() means a function returning int that takes any number of parameters. Thus, if you have a function that takes no parameters in C, the former is the correct prototype.

In C++, I believe int f(void) is deprecated, and int f() is preferred, as it specifically means a function that takes no parameters.

like image 197
Chris Young Avatar answered Sep 20 '22 11:09

Chris Young