+ 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?

28th Jun 2021, 6:05 PM
Aswin Kumar S
Aswin Kumar S - avatar
3 Respuestas
+ 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); ```
28th Jun 2021, 6:37 PM
XXX
XXX - avatar
+ 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
28th Jun 2021, 6:37 PM
Luis Fabricio Tano
Luis Fabricio Tano - avatar