+ 2
Is passing void as a argument to a function same as not passing any kind of arguments in C programming?
For example, int main(void) {....} and int main() {....} Are those two same or not in C programming?
3 ответов
+ 3
They are the same. As far as I know, specifying void as parameter is generally considered better practise because
1. It explicitly says "no arguments"
2. A function declared with empty parameter list (like int main()) can be passed arguments. This may lead to confusion. But a function with void as parameter (like int main(void)) won't allow this.
(bad) This works:
```
int foo();
foo(10);
```
(good) but this doesn't
```
int foo(void);
foo(10);
```
+ 3
Hi Aswin:
"
f() and f(void) for definitions
f() {}
vs
f(void) {}
are similar, but not identical.
6.7.5.3 Function declarators (including prototypes) says:
14 An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
which looks similar to the description of f(void).
But still... it seems that:
int f() { return 0; }
int main(void) { f(1); }
is conforming undefined behavior, while:
int f(void) { return 0; }
int main(void) { f(1); }
is non conforming as discussed at: Why does gcc allow arguments to be passed to a function defined to be with no arguments?"
From: https://stackoverflow.com/questions/693788/is-it-better-to-use-c-void-arguments-void-foovoid-or-not-void-foo