views:

894

answers:

12

Can i write a program in C or in C++ with two main functions?

+7  A: 

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.

Mike
+8  A: 

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.

Prasoon Saurav
+13  A: 

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.

Uri
+3  A: 

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.

tgiphil
+9  A: 

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.

Roger Pate
The link to "weak symbols" does not really support your assertion, the final program would still only have one entry point to one main() function. The concrete main symbol would simply override the weak one.
Clifford
"Can I write a program in C or in C++ with two main functions?" Yes, the source code, which is the program in C/C++, will have two main functions. The compiled version, which is the program in machine code, will still have two main functions. The linked version, which is actually run on the computer, will only have one. However, the real point of my answer is that **he's asking the wrong question**, and the answer to it almost certainly *doesn't matter*, and what he should do is ask a different question.
Roger Pate
+1  A: 

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 ;)

Kamran
+2  A: 

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).

Phong
Wouldn't that just be nine separate programs rather than one program with nine entry points!?
Clifford
What you say is true, but it would be more accurate to represent a SPE as a "physical thread" where the SPE main function would be the entry point of the thread. (this would become MultiProcess Multi-Thread debate ^^)
Phong
+10  A: 

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.

Johannes Schaub - litb
Don't you need to return explicitly in the 'real' main() as well? Right now it's not defined what result code your posted program will return (AFAIK)
Jeremy Friesner
No, the real main() does not require a return value, it defaults to 0.
Chinmay Kanchi
Chinmay: To be precise, ::main doesn't require a return *statement*, where it defaults to `return 0;` (`return;` is not valid). And the return type must remain int regardless. And using this in any way other than to point out how ::main is special is dubious, YMMV.
Roger Pate
The name really is special and reserved, but it's `::main` instead of `main` (just like `::_a` is reserved but `ns::_a` is not), where I'm not sure the OP really understood that he could make this distinction, much less that he should.
Roger Pate
@Roger, the definition and use of "name" in the Standard is pretty confusing. Actually, clause 3 defines "name" as "the use of an identifier, conversion-function-id, template-id, ... that denotes an entity or label". And it defines equivalence between names like "Two names are the same if they ... are identifiers composed ... are conversion-function-ids having the same type...". It's not defined to include their qualifiers (i.e "qualified name" means "a name preceeded by a qualifier", not a "name that includes ::"). But the Standard uses it inconsistently (like in the template chapters)
Johannes Schaub - litb
So i'm using "name" as it is defined - not including any qualifiers and stuffs: The name in "::main" is "main", and it's preceeded by a "::". The template chapter and some paragraphs about name lookup uses "name" as "::main" - i.e all parts of names + qualifiers. It's a defect in my opinion, and i spoke to others that feel the same way.
Johannes Schaub - litb
@Roger, reserved is the name `_a` - if it is declared in the global namespace. There is no name `::_a` if we keep ourself strictly to the definition of "name". See 17.6.3.3.2/1.
Johannes Schaub - litb
@Roger, and notice that you are allowed to declare `struct main { };` in the global namespace, i think - disproving the statement that `::main` would be a name reserved for the main function :)
Johannes Schaub - litb
A: 

No,The main() is the entry point to your program,since u can't have two entry points you cant have two main().

William
A: 

You can write it, and it'll compile, but it won't link (unless your linker is non-comformant)

Terry Mahaffey
A: 

The idiom is to dispatch on the value of argv[0]. With hardlinks (POSIX) you don't even lose diskspace.

just somebody
A: 

If one is static and resides in a different source file I don't see any problem.

ntd