Beruflich Dokumente
Kultur Dokumente
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.
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;
}
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
In C (unlike C++), a function that doesn't take any parameters is declared as int
myFunc(void)
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.
Jonathan Valvano
this is a standard from the old DOS IBM-PC. When the main program finishes it
returns true if it is a normal return, and false for error return, For an embedded
systems we do not return, so it doesn't matter