+ 4

Difference between int main and void main?

25th Aug 2016, 6:39 AM
K N Agarwal
K N Agarwal - avatar
17 Respostas
+ 9
int and void are both variable classes. Int is an integer, void is literally nothing. A function named int function returns an integer to the caller, void function at the same time doesn't return anything. I suppose, meanwhile not completely sure that void main is NOT a valid function in C++ for most IDE and architecture. Arduino, for example, using it, because there is no caller of main routine.
25th Aug 2016, 7:17 AM
Tamas Szekffy
Tamas Szekffy - avatar
+ 7
int main returns a value(typically you see return 0 at the end of a main function) void main doesn't return a value (hence the name "void" , since void means nothing) This is what i mean: int main() { // Code here return 0; // returning 0 tells the computer stop, and also says that the // program has run successfully. You can also do return 1, or -1 // but they have different meanings } void main() { // Code here // Returns nothing, since it's a void function }
20th Dec 2016, 3:56 PM
hindstrust
+ 3
int main returns a variable when main function run while void main does not... like if we write void display (c) { cout <<''multiplication is:''<<c; } int main() { int a=10; int b= 10; int c=a*b ; return c; void display (); } and in case if made void main then void main() { int a=10; int b= 10; int c=a*b ; cout <<''multiplication is:''<<c; }
19th Dec 2016, 3:53 PM
Isha
+ 2
Pls ans with a example
25th Aug 2016, 7:21 AM
K N Agarwal
K N Agarwal - avatar
+ 2
void noanswer () { //program code here return 0; } int answer () { //program code here return 1; } int main () { std::cout << " Return of void: " << noanswer() << std::endl; std::cout << " Return of int: " << answer() << std::endl; return 0; } After compile, it will return: Return of void: Return of int: 1 (I'm on the go, can't test the code.)
25th Aug 2016, 7:29 AM
Tamas Szekffy
Tamas Szekffy - avatar
+ 2
void gives null value to you and if you are using void main than u shouldn't have to return the value instead of it write getch();
2nd Sep 2016, 11:12 AM
Ayush Jaiswal
+ 2
int main means the function main has a data type that is int and it will return a integer value. and void main means the function main has a data type void .and the function main will not return any value.
17th Nov 2016, 5:33 PM
D E E P A N S H U YADAV
D E E P A N S H U YADAV - avatar
+ 2
it's return type dude void means nothing will return and int means integer value will return
20th Dec 2016, 2:02 PM
Mohan Chandra
Mohan Chandra - avatar
+ 1
Pls ans??
25th Aug 2016, 6:42 AM
K N Agarwal
K N Agarwal - avatar
+ 1
By the way, a code example may help a lot to precisely answer your question.
25th Aug 2016, 7:18 AM
Tamas Szekffy
Tamas Szekffy - avatar
+ 1
Will not compile because a function call for a return (cout) without a function without return is not valid.
25th Aug 2016, 7:33 AM
Tamas Szekffy
Tamas Szekffy - avatar
+ 1
very good is your answer
25th Aug 2016, 9:38 PM
abolfazl
abolfazl - avatar
+ 1
int main returns a value but void main dont return a value
8th Dec 2016, 1:23 PM
Cenk Camkıran
Cenk Camkıran - avatar
+ 1
int main returns a value whereas void main don't return a value its simply nothing that's why after int main we use return statement whereas in void main we don't .
19th Dec 2016, 5:14 AM
Abhishek Saxena
Abhishek Saxena - avatar
+ 1
why we use getch()
19th Dec 2016, 7:45 PM
Salwa Alvi
Salwa Alvi - avatar
+ 1
int main() can return an error level, whereas void main() doesn't return anything. Also, it is best to use int main() for error checking, and some compilers won't compile runnable programs with void main()
20th Dec 2016, 7:32 AM
Gabe Rust
Gabe Rust - avatar
+ 1
just assume main as function. then int main will need a return value whereas void main does not need any of such things. that is why when we use int main we have to add a return statement in the last whereas in void main there is no return statement needed.
21st Dec 2016, 1:00 AM
Ateendra Gaur
Ateendra Gaur - avatar