What is the purpose of int main() in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

What is the purpose of int main() in C++

I just started learning C++ and I don't really understands what is the main function, what does it do and why do we need it? Can't we just have different functions, like in Python?

28th Dec 2016, 10:56 AM
Gami
Gami - avatar
4 Answers
+ 6
Main() is the main function, everytime your program gets executed, the main() function is called. You can call other functions too, but you have to call them inside the main(). Also make sure to return 0 at the end of the main(), this will indicate that the program has finished successfuly.
28th Dec 2016, 11:14 AM
Uran Kajtazaj
Uran Kajtazaj - avatar
+ 5
Basicly when your program starts running, the first thing it looks for is the programs main function (so it knows where to start execution).
28th Dec 2016, 2:17 PM
Faris Hasković
Faris Hasković - avatar
+ 2
The main function is where the compiler begins the execution of a program.It can be given a type into or it can be void. It is void when the function doesn't return anything
31st Dec 2016, 12:22 AM
Max Otuteye
Max Otuteye - avatar
+ 1
Pardon me, as I want to ask some Questions. My students asked me why it is int main() not something like void main()? I dont know how to answer tho. Any explanation?
4th Oct 2018, 10:23 AM
Khadam