how can we have two main function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divakarpandiyan
    New Member
    • Feb 2008
    • 3

    how can we have two main function

    Dear All,
    As all know C does not support Polymorphism concept. And we can't declare two function with same name then how we can have two main function
    for ex
    main ();
    or

    main (argv,argc[])
    then how it is possible. Even printf is also work in the same concept. Printf is a function which can take multiple parameter and that too different parameter

    Can you help me in this....
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by divakarpandiyan
    Dear All,
    As all know C does not support Polymorphism concept. And we can't declare two function with same name then how we can have two main function
    for ex
    main ();
    or

    main (argv,argc[])
    then how it is possible. Even printf is also work in the same concept. Printf is a function which can take multiple parameter and that too different parameter

    Can you help me in this....


    printf declartion is
    int printf(char *args,...);
    So you can pass anything in the args string.
    This is not overloading.
    printf is use of variable argument list.
    You can go for 2 mains if you can put the main in #ifdef and supply the variable using -D while compiling the code

    Raghuram

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by divakarpandiyan
      As all know C does not support Polymorphism concept.
      It does if you use function pointers in your structs. It's just that C++ makes it easy whereas in C you have to do a lot more work. B ut I have seen polymorphic C programs in operation.

      Setting up a VTBL is just not that hard.

      Comment

      Working...