Page 1 of 1

possible to have 2 main in c

Posted: Thu Aug 12, 2004 4:47 am
by selva
can we have 2 main functions in a c program.. if possible tell me how to do ???

Re:possible to have 2 main in c

Posted: Thu Aug 12, 2004 5:01 am
by Legend
What do you want to do with that "feature" if it would exist?

Re:possible to have 2 main in c

Posted: Thu Aug 12, 2004 5:06 am
by greetselva
just for a MATTER of fact :)

Re:possible to have 2 main in c

Posted: Thu Aug 12, 2004 11:11 am
by zloba
yeah sure, you can have a hundred of main()s.
well it won't compile, of course, but don't let that stop you :)

seriously, if you're trying to do something fancy, you should look at c++: namespaces, classes, static functions, virtual methods etc.
or maybe you can make do with function pointers in plain C.

"when all you have is a hammer, every problem starts to look like a nail"

Re:possible to have 2 main in c

Posted: Thu Aug 12, 2004 1:10 pm
by Candy
if you make a program that allows you to start at any arbitrary address, you can have thousands of mains. If you make a program that starts all main-like functions with a main-like name, you can have as many as you like. If you want to make a usable application, use only this one entry point. It makes life a lot easier.

Re:possible to have 2 main in c

Posted: Thu Aug 12, 2004 11:10 pm
by Guest
If it is possible to have more than one main function, could you please explain, how to do so?

Thanks in advance.

Re:possible to have 2 main in c

Posted: Fri Aug 13, 2004 12:25 am
by Candy
Guest wrote: If it is possible to have more than one main function, could you please explain, how to do so?

Thanks in advance.
For Linux (example):

Code: Select all

int main(int argc, char **argv) {
    if (fork()) {
        main1(argc, argv);
    } else {
        main2(argc, argv);
    }
}
upon execution, both mains are started. Having two functions with the same name? Not without mangling, and you can only have one entry point in most binary formats.

Re:possible to have 2 main in c

Posted: Tue Sep 14, 2004 7:01 pm
by Andrew_Baker
There's no practical use for two mains, anyway. You could do it just to be clever, but cleverness doesn't make for a good code.

"when all you have is a hammer, every problem starts to look like a kitten"

Re:possible to have 2 main in c

Posted: Tue Sep 14, 2004 11:54 pm
by Candy
Andrew_Baker wrote: "when all you have is a hammer, every problem starts to look like a kitten"
That's animal abuse! And, misquoting.

"when all you have is a hammer, every problem starts to look like a nail"

Re:possible to have 2 main in c

Posted: Wed Sep 15, 2004 12:47 am
by distantvoices
In C I dare say having two main() functions is 1. waste, 2. food for the compiler to complain a lot about. Which of the two shall it link against crt0.o? Ha?

So, short conclusion: Forget about using 2 main() in a c program.

@andrew_baker:weird humour is your mark, it seems *g*. A look at voodoo girl proves that. but anyway, don't throw hammers at kittens. they are so cute. Nails are better.

Re:possible to have 2 main in c

Posted: Wed Sep 15, 2004 10:16 am
by mpp.eox3
beyond infinity wrote:Nails are better.
I agree, stabbing rusty 9" nails through them is less messy than crushing them with a hammer ;]

Rated R.