Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
is it possible to have more then one main functions in c language in any condition.
if it is then how we can do that....
Posted
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 2:35am    
Why?!
—SA
CPallini 16-Oct-13 6:23am    
Yes, exactly. My 5.
Sergey Alexandrovich Kryukov 16-Oct-13 9:31am    
Thank you, Carlo.
I did not expect so many answers to this question. :-)
—SA

the Main function is the point of the program where its execution starts. Consequently, only one Main function can exist in any executable.
But in your source code, you may have more than one Main function. And with preprocessor directives, all of them with the exception of exactly one Main function are removed when the executable is built.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 2:38am    
Exactly, a 5.
I added my 5 cents in my answer, please see.
—SA
In addition to the other comments, it is possible at link time to specify the program's entry point[^], but you need to understand the potential for problems.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 9:30am    
Good point, good to know, a 5, but it would not make two main functions. OP's problem is rather some logic behind the possible scenarios.
—SA
Richard MacCutchan 16-Oct-13 9:52am    
Quite so, but it is also possible that this is the issue. As usual a vague question can have many answers.
Sergey Alexandrovich Kryukov 16-Oct-13 12:01pm    
I have some guesses I just added to my answer, but I don't like the idea of making any guesswork, did it just because nobody mentioned this aspect, please see.
—SA
The only language in which I've used "multiple main" functions is Java. There you can put a public static void main(String[] args) into an class and you can start the main() function of any class from the IDE (for example Eclipse). This can be useful to put in small test cases into the classes. Still, when you finally package your product you have to mark one of these classes as the main class (like com.mycompany.MainClass) in your manifest file and when someone starts your program (for example from a jar file) then only one main is valid.

In C you don't even have namespaces (like packages and classes in java) so you simply can't implement features like this. If you want test functions in several places (like in java) then you can do that this way:
Something.c:
C++
... blah blah normal release code of Something.c ...


#if BUILD_DEBUG
void MyTestCode_2()
{
    ... blah blah blah ...
}
#endif

main.c:
C++
int main(int argc, char* argv[])
{
#if BUILD_DEBUG
    // this stupid if helps avoiding "unreachable code" warnings in most compilers
    // and helps you to turn on/off debug test code
    if (1)
    {
        // you can declare and call any of your test functions here
        void MyTestCode_2();
        MyTestCode_2();
        return 0;
    }
#endif

    ... original main code ...
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 9:26am    
Nice explanation, a 5.
—SA
pasztorpisti 16-Oct-13 9:43am    
Thank you!
In addition to Solution 1:

Your problem is the way you approach asking questions. Certainly, the whole idea to use multiple entry points is based on some misconception, quite a big on. But what is it? We don't know. Therefore, it's always a good idea to share your thinking and explain your ultimate goals. It would give us a chance to really help you, by explaining the misconception and suggestion a really working alternative.

[EDIT]

For example, if you meant to have this application started in different environments or by different applications, the answers would be "it's wrong to start a separate process without a good reason, instead, make your module a DLL (shared library), not application".

If you means to run this application in different ways, depending on how the user specifies it, the answer would be "use the command line".

And so on…

—SA
 
Share this answer
 
v2
You can have 2 or more main() functions in your source code anyway. Even so, only one of them will be compiled. One practical case is if you're going cross-platform.

C++
//sample.c
#define WIN32

#ifdef WIN32
int main()
{
  //in this case only this main() will be compiled.
}
#endif

#ifdef LINUX
int main()
{
  //another main for linux platform
}
#endif

#ifdef UNIX
int main()
{
  //another main for unix platform
}
#endif


In the above application there're 3 main() functions defined. But only the main() at the top will be compiled.

Your application's source code can contain more than one main() function and one of them must be chosen before compiling with the help of the pre-processor.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 9:29am    
Imagine there is "overloading". What, would it change the situation? This post it totally irrelevant, sorry. It needs some logic though: "main" in the question does not imply the name of the function, but function of this function.
—SA
Captain Price 16-Oct-13 10:08am    
...oO. Yes, it seemed irrelevant, only a fact. I updated my post.
Your question is same as asking can I have multiple functions of the same name in a C program? The answer is no, as the compiler will not which one to choose when the name is encountered. Similarly to questions like "multiple declaration of variables of the same name" etc. It should be detected as errors by the compiler. Hope it helps :-).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Oct-13 11:56am    
I did not vote; all your statements are correct, it's just not likely that this is what OP was asking about.
Can you see the difference?

"multiple main functions in c language"

or

"multiple 'main' functions in c language"?

OP's variant is the first one, which would be more logical: not the proper name of the function, "main", but the adjective "main", meaning several entry points. And this is just the misconception of the scenarios of the application use, not of the compilation.

—SA
Member 13329283 3-Aug-17 6:56am    
Hello , I started one project that is converting .bmp file to .jpeg file without change in pixels but only size has to reduce for that, i required initially converting .bmp file to RGB and then RGB to YCbCr ...I thought instead of writting code if anyone already written that code please provide me.. please help me for the same.. thank you,...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900