-3

I study C both from textbooks and websites. Somewhere the sample codes begin the program execution by "void main" and somewhere they begin the function with "int main". What's the difference, really? They both give the same result. Is there anything that makes them slightly different? If so, please mention what does "void" does, and what "int" does.

Soha Farhin Pine
  • 313
  • 6
  • 16
  • Who downvoted my question?? Why??? – Soha Farhin Pine Sep 30 '15 at 10:57
  • 2
    void functions returns nothing, int functions must return int.. – Akın Yılmaz Sep 30 '15 at 10:58
  • it's difference between return type if you use int main you will return integer value before close main function but when use void you not return anything. – Chanom First Sep 30 '15 at 11:00
  • No I didn't copy any question. I just gave my one. – Soha Farhin Pine Sep 30 '15 at 11:00
  • "please mention what does "void" does, and what "int" does" Finding a C book or tutorial too difficult? – too honest for this site Sep 30 '15 at 11:05
  • As a beginner, you can treat them as equivalent. The program will do the same stuff. The difference is the return value that is provided to the operation system. If you start your program on command line, after program termination you can access the return code. If the returned code is zero, it indicate that the program run successful (this will also be returned if you use the `void` signature). All other values indicate an error in the program. ./runVoidMain echo $? -> 0 ./runIntMainWithReturnZero echo $? -> 0 ./runIntMainWithReturnOne echo $? -> 1 – Matthias J. Sax Sep 30 '15 at 11:06
  • I have books that use only one of the either ways. No book explained that matter. – Soha Farhin Pine Sep 30 '15 at 11:13
  • Hey guys can you vote for undeleting this question : http://stackoverflow.com/questions/31493309/total-number-of-lines-in-notepad-or-sublime?noredirect=1#comment50950505_31493309 – Soha Farhin Pine Sep 30 '15 at 11:23

2 Answers2

3

The int main() and the void main() all do the same, which is process the main process of the program.

void main() it means that the functions main() does not return a value.

The difference is int main() is the only right way of calling it, since every program, when called, returns an "error message" that the OS interprets, in which case, closing the program with a return 0; tells the process calling your program that it ended without a problem.

Animesh Kumar Paul
  • 2,241
  • 4
  • 26
  • 37
0

The ANSI standardization of C says that the main function must return an integer. But if you program embedded systems, for instance, then you will use void main. Please se this question for elaboration: What should main() return in C and C++?

Community
  • 1
  • 1
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • I would upvote if you hadn't added that line about embedded systems, because it's sorely misleading. – DevSolar Sep 30 '15 at 11:00
  • Could you plz elaborate on this matter?? – Soha Farhin Pine Sep 30 '15 at 11:00
  • @DevSolar How is it misleading? The format of main on an embedded system is not specified by the standard. – Lundin Sep 30 '15 at 11:03
  • And what does the actually **relevant** ISO standard say? – too honest for this site Sep 30 '15 at 11:03
  • @Olaf Depends on which version of the standard, and if it is a hosted or freestanding system, [see this](http://stackoverflow.com/a/31263079/584518). – Lundin Sep 30 '15 at 11:04
  • @Lundin: The standard states that in a hosted system there shall be a function `main`, and it shall return `int`. There are no specifications as to what a freestanding system does, so stating that "you will use `void main`" is misleading. – DevSolar Sep 30 '15 at 11:07
  • @Lundin: There is only one valid version of the standard and that is commonly called "ISO", just because the ANSI is a national organisation. And, as you already commented, the standard does not specify a signature for freestanding implementations; not `void main(void)` as the answer states.. – too honest for this site Sep 30 '15 at 11:08
  • @DevSolar: Not exactly. The standard goes one step further and states that a [freestanding](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.1p1) environment may use any signature it wants. – too honest for this site Sep 30 '15 at 11:10
  • Standards aside, I have yet to see an embedded system where the form of main was not `void main (void)`. Systems that don't have a main() at all, but just a reset vector are usually written in pure assembler. – Lundin Sep 30 '15 at 11:13
  • Plz can you stop this fighting!! – Soha Farhin Pine Sep 30 '15 at 11:14
  • @Lundin: [OSDev BareBones kernel tutorial](http://wiki.osdev.org/Bare_Bones). You asked. ;) – DevSolar Sep 30 '15 at 11:15
  • Hey guys can you vote for undeleting this question : http://stackoverflow.com/questions/31493309/total-number-of-lines-in-notepad-or-sublime?noredirect=1#comment50950505_31493309 – Soha Farhin Pine Sep 30 '15 at 11:23