Cr7 is live
3 months ago
Cr7country asked

Can we use void main instead of int main?

Kelish Rai
Expert
2 months ago
Kelish Rai answered

Yes, you can technically use void main() instead of int main(). However, it's not recommended.

According to the C++ standard, the correct and portable way to define the main function is:

int main() {
    // your code here
    return 0;
}

This version is universally accepted and ensures that your program can properly communicate with the operating system. The return 0; statement signals that the program finished successfully.

If you use void main(), you won't be able to use return 0; because the function doesn’t return anything. That might not cause an error in some compilers, but it's considered non-standard and could lead to unpredictable behavior or reduced portability.

In short:

  • Use int main() for standard, portable, and reliable C++ code.

  • void main() might work in some environments, but it’s not guaranteed.

C++
This question was asked as part of the Learn C++ Basics course.