Avatar ·

What should the main() function return in C++: void, int, or something else?

Hello everyone! I recently started learning C++ and I'm confused about the `main()` function. In different sources I've seen different declarations: some write `int main()`, some write `void main()`, and sometimes even just `main()` without specifying a type. I tried writing a simple program: ```cpp #include using namespace std; void main() { cout << "Hello, world!" << endl; } ``` In Visual Studio this compiled and even worked, but on another compiler (g++) it gave an error: `'::main' must return 'int'`. Why is that? What should `main()` actually return? And do I need to return something at the end if I write `int main()`? Will it work if I don't write anything in `return`? Also, another question: what exactly does `main()` return and who needs it? Please explain for a beginner.