Howdy, Stranger!

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


Basic one

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.


  • BbloxBblox Posts: 1
    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,519
    [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]
  • 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,519
    [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.