possible to have 2 main in c

Programming, for all ages and all languages.
Post Reply
selva

possible to have 2 main in c

Post by selva »

can we have 2 main functions in a c program.. if possible tell me how to do ???
Legend

Re:possible to have 2 main in c

Post by Legend »

What do you want to do with that "feature" if it would exist?
greetselva

Re:possible to have 2 main in c

Post by greetselva »

just for a MATTER of fact :)
zloba

Re:possible to have 2 main in c

Post 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"
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:possible to have 2 main in c

Post 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.
Guest

Re:possible to have 2 main in c

Post by Guest »

If it is possible to have more than one main function, could you please explain, how to do so?

Thanks in advance.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:possible to have 2 main in c

Post 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.
Andrew_Baker

Re:possible to have 2 main in c

Post 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"
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:possible to have 2 main in c

Post 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"
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:possible to have 2 main in c

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
mpp.eox3

Re:possible to have 2 main in c

Post 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.
Post Reply