What is the correct way, according to the latest C standard, to define functions without parameters: int main()
or int main(void)
?
A parameterless method is a function that does not take parameters, defined by the absence of any empty parenthesis. Invocation of a paramaterless function should be done without parenthesis. This enables the change of def to val without any change in the client code which is a part of uniform access principle.
int main (int argc, char *argv) A main() function can be called using command line arguments. It is a function that contains two parameters, integer (int argc) and character (char *argv) data type. The argc parameter stands for argument count, and argv stands for argument values.
Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.
Both forms of definition are valid (the one without void
is an invalid prototype and an incomplete (albeit valid) declaration).
The form int main(void) { /* whetever */ }
also provides a prototype for the function.
The form int main() { /* whatever */ }
does not provide a prototype (and the compiler cannot check if it is called correctly).
See the Standard (PDF)
6.7.5.3/14
An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters.
difference between definition: int main() { /* whatever */ }
and declaration: int main();
and prototype: int main(void);
.
The definition does not provide a prototype;
the declaration is valid but specifies no information about the number or types of parameters;
the prototype is ok and compatible with the definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With