问题
I've been learning C programming in a self-taught fashion for some weeks, and there are some questions that I have concerning the main()
function.
All functions must be declared in their function prototype, and later on, in their defintion. Why don't we have to declare the
main()
function in a prototype first?Why do we have to use
int main()
instead ofvoid main()
?What does return 0 exactly do in the
main()
function? What would happen if I wrote a program ending themain()
function withreturn 1;
, for example?
回答1:
- A declaration of a function is needed only before a function is used. The definition is itself a declaration, so no prior prototype is required. (Some compilers and other tools may warn if a function is defined without a prior prototype. This is intended as a helpful guideline, not a rule of the C language.)
- Because the C standard says so. Operating systems pass the return value to the calling program (usually the shell). Some compilers will accept
void main
, but this is a non-standard extension (it usually means "always return zero to the OS"). - By convention, a non-zero return value signals that an error occurred. Shell scripts and other programs can use this to find out if your program terminated successfully.
回答2:
1) All functions must be declared in their function prototype, and later on, in their definition. Why don't we have to declare the main() function in a prototype first?
Not true. Simple example:
void foo(){} //definition
int main()
{
foo();
return 0;
}
Only when one function is called but the definition isn't seen yet, a declaration is required. That will never happen to main
since it is the starup of the program.
2) Why do we have to use int main() instead of void main()?
Because the standard says so. (To be more precise, it's true on a hosted environment, which is usually the case)
C99 5.1.2.2.1 Program startup
The function called at program startup is named
main
. The implementation declares no prototype for this function. It shall be defined with a return type ofint
and with no parameters:int main(void) { /* ... */ }
or with two parameters (referred to here as
argc
andargv
, though any names may be used, as they are local to the function in which they are declared):int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
3) What does return 0 exactly do in the main() function? What would happen if I wrote a program ending the main() function with return 1, for example?
The return value indicates the result of the program. Usually 0
indicates success while other values indicates different kinds of failure.
回答3:
You are not free to chose the return type of main because you did not write the code that calls main. The code calling main already existed before you even thought about learning C. It was written by the folks providing the C runtime startup code, which usually is linked automatically to your executable without you knowing. This code often resides in a file called crt0.o (created from crt0.c or even assembler in crt0.s). It expects to use a return value indicating success (0) or failure (nonzero), plus possibly other information like whether the code was terminated due to a signal and if so, which one. These are bits of Unix history, that I won't repeat here :-)
回答4:
1) Not necessarily; a definition also serves as a declaration. Secondly, there are only a few valid signatures for main
anyway, and you normally won't call main
within your code unless you're writing an entry for the IOCCC.
2) Short answer: because the language definition says so. Longer answer: this is how your program indicates success or failure to the host environment. An individual implementation is free to support additional signatures for main
, but it must document those additional signatures. If your compiler documentation does not list void main()
as a legal signature, then you shouldn't use it.
3) By convention (at least on *nix systems where C was first used), a status of 0 indicates success, and a non-zero status indicates ... something other than success. Exactly what value corresponds to what status is up to the implementation.
回答5:
1) it is false, you can only create the defintion of a function.
2) we can know if the main() function correctly terminate
3)the same except that in your shell it will be writed 1 instead of 0
回答6:
1.The main() function is implicitly called by the C library by recognizing the in-built keyword 'main'. So we don't need to declare a prototype for main function .
2.This I am not sure, but I think it depends on the type of editor used . In Turbo C , void main() will be accepted, whereas in dev-cpp main() should return a value.
3.return 0 simply exits the program with exit status 0 , in other words the return value determines the exit status of the main thread.
回答7:
Simply put most essence for all of your questions is traditions and conformance. Toolchains, operating systems, etc know that way that this procedure called main(), must be called out first from the user code space(program)...
Now specifically: 1) Because of the conformance as I said. You do not need to declare because toolchains and operating systems know already about main. Also there are other conformance functions like exit().
2) When the main some time returns then the operating system can have the result back from it. Usually non zero means error. So when U are using scripts or other programs calling out your program e.g. main() function, you can check if it was successful.
3) Return something else than zero means error. But actually you can interpret that value how you want it. But as I said OS can have the result.
Additional info: main() is actually not THE FIRST function (you have written) that will be called out when you start the program. BUT actually operating systems and tool chains facilitate other calls before your main, to setup environment, do the initialization or whatever. But you do not know about that directly when you are writing your code and you do not have to deal with that and think about that at all. In embedded systems there will be usually some very low level functions called to setup the CPU main clock, interrupts, stack, etc. Some of the tool chains like IAR actually can enable you to execute your own code before main is called.
Hope this helped :)
回答8:
Functions need not necessarily be declared first as a prototype. Such a declaration is needed only if we need to use a function before it is defined.
main has type int by definition.
The meaning of the value returned from main is conventional. The convention generally accepted is that 0 is considered success, and not 0 some kind of failure.
回答9:
When we run a C program, Computers control passes over to the C programs main() function, From there itself the C program starts to execute
来源:https://stackoverflow.com/questions/18446686/main-function-in-c