Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard way to define parameter-less function main() in C

Tags:

c

What is the correct way, according to the latest C standard, to define functions without parameters: int main() or int main(void)?

like image 845
Paul Manta Avatar asked Nov 05 '11 18:11

Paul Manta


People also ask

What is parameter less function in C?

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.

How do you declare a main function in C?

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.

What is the main () function in C?

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.


1 Answers

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.

like image 166
pmg Avatar answered Oct 26 '22 23:10

pmg