Can i write a program in C or in C++ with two main functions?
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.
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.
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: 1) 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.
2) Create a library out of the shared code and compile each main file against that library. You'll end up with two executables.
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.
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.
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 ;)
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).
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.
No,The main() is the entry point to your program,since u can't have two entry points you cant have two main().
You can write it, and it'll compile, but it won't link (unless your linker is non-comformant)
The idiom is to dispatch on the value of argv[0]
. With hardlinks (POSIX) you don't even lose diskspace.
If one is static
and resides in a different source file I don't see any problem.