-9
#include <stdio.h>
#include <string.h>

int main1(void)
{
   printf("Came to main1.\n");
}

int main2(void)
{
   printf("Came to main2.\n");
}

int main(void)
{
   printf("Came to main. \n");
}

I have main1, main2, and main. I want to see main1 output, but every time I execute the program, I can only see main output

cs95
  • 379,657
  • 97
  • 704
  • 746
tomliang
  • 37
  • 4
  • 3
    By calling them as you would any other function: `main1(); main2();`, but explicitely from either the actual `main`, or another function that the main calls. – AntonH Jun 05 '17 at 20:10
  • 2
    `main` is the only function that gets called automatically. If you want to call another function, you need to explicitly call it yourself. – dbush Jun 05 '17 at 20:10
  • 7
    Sounds like you need a [good C++ book](https://stackoverflow.com/a/388282/6525260) – Arnav Borborah Jun 05 '17 at 20:11
  • 2
    Or a [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) (let's not discriminate, OP put both tags ;) ). – AntonH Jun 05 '17 at 20:13
  • 1
    @AntonH, that is true. This is a completely valid program in C, for that matter. I think the OP accidently tagged it C++ – Arnav Borborah Jun 05 '17 at 20:15
  • Well, I guess another answer would be to thread off the execution of main1 and main2, but I suspect that's not what the OP was after:( – ThingyWotsit Jun 05 '17 at 20:24
  • Why so many downvotes? – Pushan Gupta Jun 05 '17 at 21:22
  • In case you are looking for parallelism, check out pthreads, fork, etc. – GroovyDotCom Jun 05 '17 at 23:02

4 Answers4

5

That's because main() is always the entry point for a C/C++ program and is the only implicitly called function.

Try:

int main(void) {
   printf("Came to main. \n");
   main1();
   main2();
}
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
cadolphs
  • 9,014
  • 1
  • 24
  • 41
2

You can do this with some preprocessor trickery:

#include <stdio.h>
#include <string.h>

#ifdef MAIN1

int main(void)
{
   printf("Came to main1.\n");
   return 0;
}

#elif defined MAIN2

int main(void)
{
   printf("Came to main2.\n");
   return 0;
}

#else

int main(void)
{
   printf("Came to main. \n");
   return 0;
}

#endif

Then you define MAIN1 or MAIN2 as needed:

[dbush@centos72 ~]$ gcc -o x1 x1.c
[dbush@centos72 ~]$ ./x1
Came to main. 
[dbush@centos72 ~]$ gcc -D MAIN1 -o x1 x1.c
[dbush@centos72 ~]$ ./x1
Came to main1.
[dbush@centos72 ~]$ gcc -D MAIN2 -o x1 x1.c
[dbush@centos72 ~]$ ./x1
Came to main2.
[dbush@centos72 ~]$
dbush
  • 205,898
  • 23
  • 218
  • 273
1

I think you are just curious as a beginner that you can define multiple mains in your program. Understand that main is a special function. You cannot have several mains in your program. If you define main1 or main2 they are always going to be ordinary functions. And execution of programs will start with main(). Though there are constructors that are called before main and you can explicitly define a function to be one, but in general/default cases main is the entry point of the program.

But if you still want to call main1 before main here's how you can do:

#include <stdio.h>

int main1(void) __attribute__ ((constructor));// Note this line.     

int main2(void) __attribute__ ((constructor));

int main2(void)
{
   printf("Came to main2.\n");
}


int main1(void)
{
   printf("Came to main1.\n");

}

int main(void)
{
   printf("Came to main. \n");
}

Also note the order in which main1 and main2 are defined. main1 is defined after main2. Therefore the output is

Came to main1.
Came to main2.
Came to main.

Beware, this is a gcc specific thing.

Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
0

You failed to call your function inside the main function. One suggestion is to not name the function as main1 and main2 since it is ambiguous.

#include <stdio.h>
#include <string.h>

int main1(void)
{
   printf("Came to main1.\n");
}

int main2(void)
{
   printf("Came to main2.\n");
}

int main(void)
{
   printf("Came to main. \n");
   main1();  // this is the plain function call. 
   main2();
}

Output:

Came to main.
Came to main1.
Came to main2.

More information about the function call.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
  • 2
    How is it ambiguous? They are named different things. – Arnav Borborah Jun 05 '17 at 20:15
  • Ambiguous, I mean you don't write function name like that in translation unit where you have entry point main(). I clearly mentioned in my comment as the suggestion not to keep the name as main1 and main2. – danglingpointer Jun 05 '17 at 20:18
  • I presume you follow some basic coding standard while you work, the function name in one translation unit or module. Each function name in one translation unit carries the name of the translation unit. – danglingpointer Jun 05 '17 at 20:21
  • well, I just mentioned about the naming convention, perhaps I didn't mention that specifically in my post. – danglingpointer Jun 05 '17 at 20:31