C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Basic one Posted by shankap on 18 Dec 2009 at 1:39 AM
I'm a newbie to the C programming. I have this basic Question.
What is the difference between

void main() and int main(void)

Please explain as you can. Thanks.
Report
Re: Basic one Posted by Bblox on 18 Dec 2009 at 4:12 AM
void main() is equiv to
void main(void)
It means the main progam function takes no args and returns no value. It's usefullness is only in its side-effects, the visible things that it does while it is running.
int main(void)
means the main program takes no arguments, but does return an integer value to the program that called it.

With "int" there, somewhere in "main" should be a return statement, at least "return 0;" or "main=0;". I forget which form C uses.
The return value is often used to return a success/failure code to the program that called this function. Success is usually "no error", i.e., 0. Anything not equal to 0 then indicates that some error situation was noticed by main.
So what calls function "main"? the op-sys? Not sure what is the point of informing the opsys about a program's problems. Maybe receiving a return-value informs the opsys that it can clean up any system-allocated resources that program main requested (like extra RAM, or graphics windows), because that program is finished, has quit.
Does that make sense?

Report
Re: Basic one Posted by AsmGuru62 on 18 Dec 2009 at 4:41 AM
The return code of the process (or main() function) is used in batch (BAT) files or by parent processes. If my program invokes another EXE to run and do some processing - I can examine the return code and check if nothing failed in this child process. It is recommended to always use int main () by the C standard.
Report
Re: Basic one Posted by shankap on 18 Dec 2009 at 9:05 PM
Yeah Thanks.
But if I use void main() the compiler displaying a warning msg. And if I use int main(void) it works. I'm using gcc compiler in linux based system (but in windows both are working). Why is that?
Report
Re: Basic one Posted by AsmGuru62 on 19 Dec 2009 at 4:40 AM
Probably, because in linux there is a higher chance that your program will be called by another program or batch (script) file. With void you can't pass any information back to the caller of your program. GCC is simply stricter in this behaviour for linux.



 

Recent Jobs