Two main function

M

Malcolm

Rex said:
hai all,
itz just a query,can I have two main function in a C program(1 file)
NO, because execution starts with main, and ANSI C programs are
single-threaded. Two mains would be ambiguous.

Quite often people need to write a temporary main, for instance to test
portions of code. Just change the name of the old main to "oldmain" and the
function will never be called.
 
R

Richard Bos

itz just a query,can i have two main function in a C program(1 file)

Can you have two foo() functions in a single translation unit? Can you
have two bar() functions? No, and no. Why would main() be any different?

Richard
 
E

Emmanuel Delahaye

Rex wrote on 13/08/04 :
hai all,
itz just a query,can i have two main function in a C program(1 file)

No. main() is just another function. You can't have 2 functions with
external linkage with the same name in a C program.
 
P

Pablo

No you can't have two.
The main function is the only function in your C code that will be execute.
I imagine that you want to execute two parts of your program (two process)
"at the same time" (i mean, in executing state). It is really simple in
linux but if you are in a windows platoform and in a C program it is not
posible.

Pablo.
 
J

Jack Klein

Rex wrote on 13/08/04 :

No. main() is just another function. You can't have 2 functions with
external linkage with the same name in a C program.

If they are both in one file (one translation unit), only one of them
needs to have external linkage. But that does not change the other
issue, you can't have two functions with the same name in a single
translation unit, regardless of linkage.
 
P

Paul Heininger

Rex said:
hai all,
itz just a query,can i have two main function in a C program(1 file)

Yes, there can be two main functions in a C program. Using preprocessor
directives, only one can be compiled during a single compilation (a/k/a
translation).

Paul
 
K

Keith Thompson

Paul Heininger said:
Yes, there can be two main functions in a C program. Using preprocessor
directives, only one can be compiled during a single compilation (a/k/a
translation).

Which means that, in effect, one of them doesn't exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top