can you insert void main instead of int main? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

can you insert void main instead of int main?

I was wondering, if we can make the main function not return by using void main(). Putting return 0; is a bit waste of time.

23rd Oct 2016, 12:49 PM
koder
koder  - avatar
6 Answers
+ 1
I did it and the compiler said void main() { must return a value, I did void main() { cout<<"test"; return 0; } and it said it again. so I can't do it.
23rd Oct 2016, 6:26 PM
koder
koder  - avatar
+ 1
I was wrong because I use visual studio and that compiler accepts void so it depends on the compiler sorry
23rd Oct 2016, 6:32 PM
Tanel Helmik
Tanel Helmik - avatar
0
Most compilers will accept it and silently convert void to int. However, 'void main()' is just plain wrong and not valid C++.
23rd Oct 2016, 12:53 PM
Ullrich Franke
Ullrich Franke - avatar
0
void main is never valid C++. The standard only specifies int main. However, to not break existing code, some compilers accept void main and silently change it to int and return 0.
23rd Oct 2016, 6:37 PM
Ullrich Franke
Ullrich Franke - avatar
- 1
void datatype can be use for a function does not have to return a value. as main() does not require to return a value so void main() can be used
31st Oct 2016, 6:25 PM
Animesh Srivastava
Animesh Srivastava - avatar
- 2
/* void main() is valid and used all the time since this function is usually not used to return a value. putting in return 0; is good coding and helps with warnings */ <-- wrong answer
23rd Oct 2016, 6:19 PM
Tanel Helmik
Tanel Helmik - avatar