
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.