Possible Duplicate:
Why is the type of the main function in C and c++ left to the user to define?
I've come across many C's main function style and syntax of writing but I'm not actually getting what this syntax mean can anybody give brief on each syntax? why void? why int? why void,int as parameter?
void main() {
}
int main() {
}
int main(void) {
}
void main(void) {
}
int main(int) {
}
int main(int argc, char* argv[]) {
}
void
as the return type means the author meant to return no value of significance to the caller (invoker of the program). Depending on the operating system, this may be acceptable, or might cause subtle difficulties with whatever starts the program.void
as the parameter means that the program will not be using the conventional means of inspecting command line arguments. Some environments provide alternative ways of obtaining them; others do not. In the latter case, it means the program ignores any command line parameters.main (int)
allows the program to inspect the number of parameters passed to the program on the command line, but does not inspect their values. Such an arrangement is uncommon, but one program which does this on many implementations is the Unix/Linuxwho
command which lists logged in users, except thatwho am i
lists only the current user. Just aswho is you
does (because both have two parameters:ОК, briefly.
If you don't care about command line parameters user specified and you don't want to set an exit code you may write this:
No parameters, but want to set an exit code:
The same as previos, but in old C style:
The same as the first one, but in old C style:
If you want to know only the number of command line parameters:
And the most complete variant:
argc is a count of parameters, argv is an array of parameters and the function returns an exit code.
There are two forms of conforming implementations specified by the c standard:
There are based on two types of envrionments that the c standard defines as:
What is
freestanding Environment
& What isHosted Environment
?A freestanding implementation is one that is designed for programs that are executed without the benefit of an operating system. For Ex: An OS kernel or Embedded environment would be a freestanding environment.
A program using the facilities of an operating system would normally be in a hosted implementation.
How does a c program execute in these two environments? What is the difference?
How a C program begins execution in both these environment differs.
For an Freestanding environment, the program startup can happen by any implementation defined function. There is no requirement that even a
main()
should exist.So any of the functions definitions mentioned in the question can be valid depending upon implementation for that Freestanding Environment. And their function parameters and return values will have implementation defined meaning, So you will need to check their documentation to know their precise meanings.
Reference:
For an Hosted environment the standard mandates the program execution begins by execution of a
main()
function and it also mandates how this function will be defined.The specifications for the same are given in: