Question:How many main() in one C program is possible?
Answer:(a)Exactly one or (b)More than one
If answer is b than write easiest program having two main()s...

As far as i know, only one main function is possible !! if their would be two programs, then the compiler would be running two programs at the same time which i think is not possible !!

Have you tried typing in multiple versions and trying to compile them?
That would be the most direct way to figure out what you can and can not do...

main() is the point that the program jumps to when it starts running. It's like the starting line of a race.

While some races are handicapped and have more than one starting point, the computer is not so inclined.;)

As L7Sqr mentions above, there is nothing like a good test to see for yourself. Not only will you conclusively prove the answer on your compiler, but you will remember it far longer, because you learned it by trying it, yourself.

Sorry, you are all wrong. You may have as many mains as you want, provided they are all static (except one, of course). I am not even talking about dll tricks.

> there is nothing like a good test

Not for the questions like this. Language is defined not by the implementation, but by the standard. Tests could be wrong. Compilers could be non-compliant. Standard is flawless. It takes a language lawyer to answer them though.

Standard is flawless.

Even when it's wrong. The C standard has had contradictory clauses in the past. Imagine the confusion that ensued. ;)

>>Sorry, you are all wrong. You may have as many mains as you want, provided they are all static (except one, of course).

Nope. The only way to do that is to put each of the static functions in different *.c files and not in the same *.c file as non-static main()

Edited 5 Years Ago by Ancient Dragon: n/a

From experience with trying it, it doesn't work if you have more than one main(). main() is the leader of it all. Don't you think you would be able to do without a main() if you could have more than one??

Sorry, you are all wrong. You may have as many mains as you want, provided they are all static (except one, of course). I am not even talking about dll tricks.

Show us you proof -- with working code compilable on any C compiler.

Any, as in all, or any as in at least one?

Disclaimer: as I said above, no test constitutes the proof. My claim may only be disproved by the relevant chapter and verse of the Standard. However, the test is here.

The following compiles with the warning (and runs as expected) with gcc. I don't know if this warning is mandated.
MSVC compiles it with the warning 'static qualifier discarded', obviously failing to link. I suspect that MSVC behaviour is not compliant.

$ cat main.c

extern int foo();

int main(int argc, char ** argv)
{
    foo();
    return 0;
}

$ cat foo.c

#include    <stdio.h>

static int main()
{
    printf("static main\n");
    return 0;
}

int foo()
{
    main();
    return 0;
}

$ gcc -Wall main.c foo.c
foo.c:4: warning: ‘main’ is normally a non-static function
$ ./a.out
static main

$ gcc -v
Using built-in specs.
Target: i686-pc-linux-gnu
Configured with: /var/tmp/portage/sys-devel/gcc-4.1.2/work/gcc-4.1.2/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/4.1.2 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/4.1.2/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/4.1.2/include/g++-v4 --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --disable-multilib --enable-libmudflap --disable-libssp --disable-libgcj --with-arch=i686 --enable-languages=c,c++,treelang,fortran --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 4.1.2 (Gentoo 4.1.2 p1.1)

Edited 5 Years Ago by nezachem: n/a

Here is chapter and verse from c++ standards ISO/IEC 14882, Programming language – C++ dated 2008-10-09. Note that 3.6.1.2 states main() shall not be overloaded.

Yes, I am well aware that this is the C forum, not C++. I don't have a copy of the C standards but I'm certain it contains the same or similar verbage.

3.6.1 Main function [basic.start.main]
1 A program shall contain a global function called main, which is the designated start of the program. It
is implementation-defined whether a program in a freestanding environment is required to define a main
function. [ Note: in a freestanding environment, start-up and termination is implementation-defined; startup
contains the execution of constructors for objects of namespace scope with static storage duration;
termination contains the execution of destructors for objects with static storage duration. —end note ]
2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementation-defined. All implementations shall
allow both of the following definitions of main:
3 The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementationdefined.
A program that declares main to be inline or static is ill-formed. The name main is not otherwise
reserved.

Edited 5 Years Ago by Ancient Dragon: n/a

> Yes, I am well aware that this is the C forum, not C++. I don't have a copy of the C standards but I'm certain it contains the same or similar verbage.

It does not.

C++ has a good reason to restrict main. It is not applicable to C.

Edited 5 Years Ago by nezachem: n/a

>> Yes, I am well aware that this is the C forum, not C++. I don't have a copy of the C standards but I'm certain it contains the same or similar verbage.

> It does not

PS: This is the it.

Edited 5 Years Ago by nezachem: n/a

This article has been dead for over six months. Start a new discussion instead.