Can I write a program in C or in C++ with two main functions?
Yes , Multiple main() are allowed but not in global namespace.
"Every C++ program must have exactly one global function named main()" - Bjarne stroustrup.
Eg 1 :
namespace foo
{
int main() //Main 1
{
return 1;
}
}
int main() // Main 2
{
}
// Main 1 : namespace is foo
// Main 2 : namespace is global
// Allowed : Yes , its allowed as both the main() are present in different namespaces.
Eg 2 :
int main() //Main 1
{
return 1;
}
void main() // Main 2
{
}
// Main 1 : namespace is global
// Main 2 : namespace is global
// Allowed : No , as multiple main() in global namespace . Compile-time error is thrown : redefinition of main() ..