I'm confused about why we need to pass void into C functions:
int f(void) { return 0; } versus
int f() { return 0; } What is the proper thing to do and why?
In C, int f() is an old-style declaration. It says that f expects a fixed but unspecified number and type(s) of arguments. For example, given int f() { return 0; }, the compiler won't complain about f(42), or f("hello, world") (though the behavior of such a call is undefined).
int f(void) explicitly says that f takes no arguments.
The C23 standard, not yet published, will change this.
(C++ has different rules; Stroustrup wasn't as concerned about backward compatibility with old C code.)
It's a vestige of C.
f(void) takes no arguments, so f(1) is invalid.
f() means that the parameters are unspecified, so f(1) is valid here.
f doesn't specify any parameters, then the behavior of f(1) is undefined. Without a prototype, it's entirely up to the programmer to get the number and type(s) of the arguments correct.The void in this case just means "there are no parameters." I'm not sure whether your second case will compile, but either way I'd prefer the first case just to be as clear as possible. Of course, when calling f, you just use the empty parentheses:
int x = f();