Many are familiar with the hello world program in C:
#include <stdio.h>
main ()
{
printf ("hello world");
return 0;
}
Why do some precede the main()
function with int
as in:
int main()
Also, I've seen the word void
entered inside the ()
as in:
int main(void)
It seems like extra typing for nothing, but maybe it's a best practice that pays dividends开发者_开发问答 in other situations?
Also, why precede main()
with an int if you're returning a character string? If anything, one would expect:
char main(void)
I'm also foggy about why we return 0 at the end of the function.
The main
function returns an integer status code that tells the operating system whether the program exited successfully.
return 0
indicates success; returning any other value indicates failure.
Since this is an integer and not a string, it returns int
, not char
or char*
. (Calling printf
does not have anything to do with returning from the function)
Older versions of C allow a default return type of int
.
However, it's better to explicitly specify the return type.
In C (unlike C++), a function that doesn't take any parameters is declared as int myFunc(void)
The following has been valid in C89
main() {
return 0;
}
But in modern C (C99), this isn't allowed anymore because you need to explicitly tell the type of variables and return type of functions, so it becomes
int main() {
return 0;
}
Also, it's legal to omit the return 0
in modern C, so it is legal to write
int main() {
}
And the behavior is as if it returned 0.
People put void
between the parentheses because it ensures proper typechecking for function calls. An empty set of parentheses in C mean that no information about the amount and type of the parameters are exposed outside of the function, and the caller has to exactly know these.
void f();
/* defined as void f(int a) { } later in a different unit */
int main() {
f("foo");
}
The call to f
causes undefined behavior, because the compiler can't verify the type of the argument against what f
expects in the other modules. If you were to write it with void
or with int
, the compiler would know
void f(int); /* only int arguments accepted! */
int main(void) {
f("foo"); /* 'char*' isn't 'int'! */
}
So for main
it's just a good habit to put void
there since it's good to do it elsewhere. In C you are allowed to recursively call main
in which case such differences may even matter.
Sadly, only a few compilers support modern C, so on many C compilers you may still get warnings for using modern C features.
Also, you may see programs to declare main
to return different things than int
. Such programs can do that if they use a freestanding C implementation. Such C implementations do not impose any restrictions on main
since they don't even know or require such a function in the first place. But to have a common and portable interface to the program's entry point, the C specification requires strictly conforming programs to declare main with return type int
, and require hosted C implementations to accept such programs.
It's called an exit status. When the program finishes, you want to let the callee know how your program exited. If it exited normally, you'll return 0
, etc.
Here's where you can learn more about exit statuses.
Why do some precede the main () function with int as in:
In C (C 89 anyway, that's the one you seem to be referring to), functions return int
by default when they aren't preceded by any data type. Preceding it with int
is ultimately just a matter of preference.
Also, I've seen the word 'void' entered inside the () as in:
This is also a matter of preference mostly, however these two are quite different:
f(void)
means the function f
accepts zero
arguments, while f()
means the function f
accepts an unspecified number of arguments
.
f(void)
is the correct way to declare a function that takes no arguments in C.
It seems like extra typing for nothing, but maybe it's a best practice that pays dividends in other situations?
The benefit is clarity. If you're going to argue that less typing is good, then what do you think about naming all of your functions and variables and files and everything with as few characters as possible?
Also, why precede main() with an int if you're returning a character string? If anything, one would expect:
char main(void)
What? why in the world would you return a character string from main
? There is absolutely no reason to do this...
Also, your declaration of main
return a character, not a character string.
I'm also foggy about why we return 0 at the end of the function.
A program returns a value to the operating system when it ends. That value can be used to inform the OS about certain things. For example, your virus scanner could return 1
if a virus was found, 0
if a virus wasn't found and 2
if some error occurred. Usually, 0
means there was no error and everything went well.
The reasoning behind this was to provide some form of error reporting mechanism. So whenever a program returns a zero value, that typically means that it believes it completed its task successfully. For non-zero values, the program failed for some reason. Every now and then, that number might correspond to a certain error message. Several system API calls in C return return codes to indicate whether the processing was successful. Since there is no concept of an exception in C, it is the best way to tell if your calls were successful or not.
The "int" before the main function declaration is the return type of the function. That is, the function returns an integer.
From the function, you can return a piece of data. It is typical to return "0" if the function completed successfully. Any other number, up to 255 is returned if the program did not execute successfully, to show the error that happened.
That is, you return 0, with an int data type, to show that the program executed correctly.
It is traditional to use int main(int argc, char * argv[])
because the integer return can indicate how the program executed. Returning a 0 indicates that the program executed successfully, whereas a -1 would indicate that it had a failure of some sort. Usually, the exit codes would be very specific and would be used as a method of debugging since exceptions (in the C++ sense) don't exist in C.
Most operating systems uses exit codes for applications. Typically, 0
is the exit code for success (as in, the application successfully completed). The return value of main()
is what gets returned to the OS, which is why main()
usually returns an integer (and since 0
is success, most C programs will have a return 0;
at the end).
精彩评论