For example,
#include <stdio.h>
void foo();
int main(void)
{
foo();
foo(42);
foo("a string", 'C', 1.0);
return 0;
}
void foo()
{
puts("foo() is called");
}
Output:
foo() is called
foo() is called
foo() is called
This code compiles well(without warnings using clang) and runs well. But I wonder what happens to the values passed to foo()
? Are they pushed in to the stack or just get discarded?
Perhaps this question sounds useless, but it does make sense. For example, when I have int main()
, rather than int main(void)
, and pass some command line arguments to it, will the behavior of main()
be affected?
Also, when using <stdarg.h>
, at least one named parameter is required before ...
by ISO C. Is it possible that we can make use of such declarations as void foo()
to pass from zero to infinite arguments to a function?
I noticed that void foo()
is a "non-prototype declaration" and that void foo(void)
is a "prototype declaration" just now. Is this somewhat relevant?
Clarification
Seems that this question is marked duplicate to What does an empty parameter list mean? [duplicate](Interestingly, that question is also duplicate...). In fact, I don't think my question have anything to do with that one. It focuses on "What void foo()
means in C", but I know this means that "I can pass any number of arguments to it", and I also know it's an obsolescent feature.
But this question is quite different. The keyword is "What if". I just want to know if I pass different amount of arguments to void foo()
, just like the example code above, can they be used inside foo()
? If so, how is this done? If not, does the passed arguments make any difference? That's my question.