[C/C++] func(void)와 func()의 차이

c, c++ func(void)와 func()의 차이점
int func(void);
int func();

C, C++에서 위처럼 함수 파라미터에 void 또는 비어 있게 할 수 있는데, C, C++ 각각 의미가 다르다.

요약하면, C/C++ 모두 동일하게 함수에서 파라미터를 받지 않길 원한다면 func(void)를 사용하는게 좋다.

C

T foo( void ); // 선언, foo는 아무 파라미터도 사용하지 않는다
T bar();       // 선언, bar는 임의의 갯수의 파라미터를 사용한다 *

T foo( void ) { ... } // 정의, foo는 아무 파라미터도 사용하지 않는다
T bar() { ... }       // 정의, bar는 아무 파라미터도 사용하지 않는다
6.7.6.3 Function declarators (including prototypes)
...
10 The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.
...
14 An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.145)

- C11 standard

C에서 함수 선언에 빈 괄호()를 쓰면 임의 갯수의 파라미터를 받도록 선언하는 것이다. (Function without prototype)

이 기능은 모든 버전의 C에서 deprecate 되었고, C23부터는 지원되지 않는다.

C++

C++에서는 ()(void)의 의미가 어느 위치에서든 동일하다

만약 임의 개수의 파라미터를 받고 싶다면 foo(...); 형식으로 사용한다. (Variadic arguments)

참고자료

What is the difference between function() and function(void)?
I have heard that it is a good practice to write functions that do not receive anything as a parameter like this: int func(void); But I hear that the right way to express that is like this: int…