In C++, the main() function is the entry point for all C++ programs. It is the first function that is called when the program starts executing. The
main() function can have two different return types: void or
int.
void main() indicates that the main() function does not return any value. This is the most common way to define the
main() function.
int main() indicates that the main() function returns an integer value. This value can be used by the operating system to determine the status of the program. For example, if the program terminates normally, the
main() function will return the value 0. If the program terminates abnormally, the
main() function will return a non-zero value.
It is important to note that the main() function must always have a return type. Even if you do not want the
main() function to return any value, you should still use the
void return type.
Here are some examples of how to define the main() function:
C++
// void main()
void main() {
// Do something
}
// int main()
int main() {
// Do something
return 0;
}
In general, you should use void main() unless you have a specific reason to use
int main().
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In C++, the
main()function is the entry point for all C++ programs. It is the first function that is called when the program starts executing. Themain()function can have two different return types:voidorint.void main()indicates that themain()function does not return any value. This is the most common way to define themain()function.int main()indicates that themain()function returns an integer value. This value can be used by the operating system to determine the status of the program. For example, if the program terminates normally, themain()function will return the value0. If the program terminates abnormally, themain()function will return a non-zero value.It is important to note that the
main()function must always have a return type. Even if you do not want themain()function to return any value, you should still use thevoidreturn type.Here are some examples of how to define the
main()function:C++
In general, you should use
void main()unless you have a specific reason to useint main().