Results 1 to 7 of 7
how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?
also, "return (0)" defines the errorlevel when the program finishes. ...
- 11-03-2009 #1Just Joined!
- Join Date
- Nov 2009
- Posts
- 14
Question about C
how come sometimes I see "int main()", sometimes I see "void main()" or just plain "main()" .. what's the difference?
also, "return (0)" defines the errorlevel when the program finishes. right?
thanks
- 11-03-2009 #2
the variable type in front of a function is what kind of variable the program returns.
This should be int since you want to return Exit codes (eg 0 successfull run)
if you do void you always return 0 and therefore this is bad practice.
Hope that helped
- 11-03-2009 #3
don't forget about your parameters
Code:int main(int argc, char **argv)
- 11-03-2009 #4
- 11-03-2009 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,961
Back in the "old" days of K&R C, you could define main() as returning void, but that is now deprecated and should be defined to return an int value - the exit code for the application. The following are valid signatures for main() these days:
int main(void);
int main(int argc, char** argv);
int main(int argc, char* argv[]);
int main(int argc, const char** argv);
int main(int argc, const char* argv[]);
The second and third versions of the argument list with char* types is allowed, provided you want to modify the actual contents of the arguments at runtime (not recommended). So, the first and last two versions are preferred - the first only when you don't allow any arguments, and just want to ignore them if passed from a shell.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-03-2009 #6Linux User
- Join Date
- Jan 2006
- Posts
- 414
void main() is still ok if you're writting a kernel though, as main should never return in a kernel, or at the very least returning anything is pointless.
- 11-04-2009 #7Just Joined!
- Join Date
- Nov 2009
- Posts
- 1
It is bcoz in linux OS it takes int main () defaiult as int return type to the OS when main terminates.
Also if you not specify any return type i,e main() only then compiler will explicitly add int data type to the program and compiler......
And also return(0) is very common that main() also a function and it has int return type so, return(0) tells the linux kernel that program is terminated normally.........


Reply With Quote

