Reputation: 353
Can I write a program in C or in C++ with two main functions?
Upvotes: 32
Views: 73802
Reputation: 1
Standard C doesn’t allow nested functions but GCC allows them.
void main()
{
void main()
{
printf(“stackoverflow”);
}
printf(“hii”);
}
The o/p for this code will be -hii
if you use GCC compiler.
There is a simple trick if you want to use 2 main() in your program such that both are successfully executed;you can use define.Example-
void main()
{
printf("In 1st main\n");
func1();
}
#define main func1
void main()
{
printf("In 2nd main\n");
}
Here the o/p will be:
In 1st main
In 2nd main
NOTE:here warning conflicting types of func1 will be generated.
And yes don’t change the place of define.
Upvotes: -3
Reputation: 89749
No. All programs have a single main(), that's how the compiler and linker generate an executable that start somewhere sensible.
You basically have two options:
Have the main() interpret some command line arguments to decide what actual main to call. The drawback is that you are going to have an executable with both programs.
Create a library out of the shared code and compile each main file against that library. You'll end up with two executables.
Upvotes: 40
Reputation: 7434
If one is static
and resides in a different source file I don't see any problem.
Upvotes: 4
Reputation: 19247
The idiom is to dispatch on the value of argv[0]
. With hardlinks (POSIX) you don't even lose diskspace.
Upvotes: 0
Reputation: 11981
You can write it, and it'll compile, but it won't link (unless your linker is non-comformant)
Upvotes: 0
Reputation: 1131
No,The main() is the entry point to your program,since u can't have two entry points you cant have two main().
Upvotes: 0
Reputation: 506955
You can have two functions called main
. The name is not special in any way and it's not reserved. What's special is the function, and it happens to have that name. The function is global. So if you write a main function in some other namespace, you will have a second main
function.
namespace kuppusamy {
int main() { return 0; }
}
int main() { kuppusamy::main(); }
The first main
function is not special - notice how you have to return
explicitly.
Upvotes: 20
Reputation: 6768
In some very special architecture, you can. This is the case of the Cell Processor where you have a main program for the main processor (64-bit PowerPC Processors Element called PPE) and one or many main program for the 8 different co-processor (32-bit Synergistic Processing Element called SPE).
Upvotes: 2
Reputation: 3280
No, you cannot have more than one main() function in C language. In standard C language, the main() function is a special function that is defined as the entry point of the program. There cannot be more than one copy of ANY function you create in C language, or in any other language for that matter - unless you specify different signatures. But in case of main(), i think you got no choice ;)
Upvotes: 1
Reputation:
Yes; however, it's platform specific instead of standard C, and if you ask about what you really want to achieve (instead of this attempted solution to that problem), then you'll likely receive answers which are more helpful for you.
Upvotes: 11
Reputation: 1252
What do you mean by "main function"? If you mean the first function to execute when the program starts, then you can have only one. (You can only have one first!)
If you want to have your application do different things on start up, you can write a main function which reads the command line (for example) and then decides which other function to call.
Upvotes: 2
Reputation: 92864
No, main()
defines the entry point to your program and you must only one main()
function(entry point) in your program.
Frankly speaking your question doesn't make much sense to me.
Upvotes: 3
Reputation: 24964
No, a program can have just 1 entry point(which is main()
). In fact, more generally, you can only have one function of a given name in C.
Upvotes: 6