What is the difference between int main () and void main() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

What is the difference between int main () and void main()

22nd Mar 2019, 8:15 AM
Emmanuel Godwin
Emmanuel Godwin - avatar
10 Answers
+ 5
int main() must return a value, while void main() doesn't return a value. Example: public int myMethod() { int age = 20; Return age; } void Example: public void myMethod () { System.out.println("my name is Trevor"); } You don't have to place a "return" here because the method didn't mention any "int" or "String" type here.
22nd Mar 2019, 1:17 PM
Trevor Netshisaulu
Trevor Netshisaulu - avatar
+ 4
Both definitions work in C also, but the second definition with void is considered technically better as it clearly specifies that main can only be called without any parameter. ... So thedifference is, in C, int main() can be called with any number of arguments, but int main(void) can only be called without any argument.
22nd Mar 2019, 9:26 AM
eMBee
eMBee - avatar
+ 3
In short it tell the what the program will return in case of int it will return an integer value and in case of void it won't return anything
22nd Mar 2019, 10:45 AM
Harshit Jain
Harshit Jain - avatar
+ 2
A program has to exit with an exit code, an execution requirement that all processors have. That is why int main is returning 0 in order to mark that it ended execution, ending with -1 would result in forced exitting. Void main is just a function than can be called and int main is the actual code that is being executed when you run the program
22nd Mar 2019, 9:46 AM
Blazy
Blazy - avatar
+ 1
Nothing will be returned in void while in int the integer value will be returned.
22nd Mar 2019, 12:16 PM
Abhyuday
+ 1
Using int main() affect the return function of the program . If void main is used the program or function cannot return it value to other function wheas it can print the result within its function region wheas if int main is used it can return an int value for the same function as well as can pass the value to ofher function
12th Apr 2019, 11:07 AM
Ali Uddin
Ali Uddin - avatar
0
main() it returns a value but void main() it doesn't return a value
11th Apr 2019, 4:00 PM
Sailakshmi Janjanam
0
Sava salu
13th Apr 2019, 12:04 PM
Lamarana Dallo
Lamarana Dallo - avatar
- 1
The difference in parameters. Void functions cannot have parameters whereas others can. Void is mainly used with main function. Niether does it return any value.
22nd Mar 2019, 2:52 PM
Anshumaan Mishra