Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Basic one

shankapshankap Posts: 5Member
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.

Comments

  • BbloxBblox Posts: 1Member
    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?

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]The return code of the process (or [italic]main()[/italic] 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 [italic]int main ()[/italic] by the C standard.[/color]
  • shankapshankap Posts: 5Member
    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?
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [color=Blue]Probably, because in linux there is a higher chance that your program will be called by another program or batch (script) file. With [italic]void[/italic] you can't pass any information back to the caller of your program. GCC is simply stricter in this behaviour for linux.[/color]
Sign In or Register to comment.