Can I call the main () function within itself ?

This is a discussion on Can I call the main () function within itself ? within the C Programming forums, part of the General Programming Boards category; Hi!!, I wanted to know if i could call the main () function within itself as shown, #include <stdio.h> main ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    Can I call the main () function within itself ?

    Hi!!,
    I wanted to know if i could call the main () function within itself as shown,


    #include <stdio.h>

    main ()
    {

    int a,b,c;
    char ans;



    printf ("Enter the first number : \n");
    scanf ("%d", &a);

    printf ("Enter the second number : \n");
    scanf ("%d", &b);

    c=a+b;

    printf ("The total is :%d", c);

    printf ("Do you want to continue ?y/n\n");
    scanf ("%c", &ans);

    if (ans=="y")
    {
    main ();
    }

    else if (ans=="n")
    {
    exit ();
    }

    }

    I wanted to know if this could be done.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    29

    Calling main () within itself

    Hi !!,
    I tried compiling the code and it works but before that I had to remove the " " around y and n and replace it with single quotes.
    But i was informed that calling main () within itseld is a bad programming practise. Is it true and if so could anyone please tell me why?. I would be very grateful for the answers.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Well, from the birth of C people are not calling main inside main and will probably never. What you do is a recursion which is not a very efficient way to implement algorithms, as it may lead to lockups, unnecessary use of extensive memory as a function continuosly calls itself.
    Also this does not seem very structural programming other. Try using other functions and use recursion in them if really necessary.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > I wanted to know if i could call the main () function within itself as shown
    No
    The latest standard specifically forbids recursive calls to main.

    Because main (alone) is called from the startup code, the compiler may produce a specialised calling sequence which normal C functions do not have.

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    >not a very efficient way to implement algorithms

    it in fact is a very efficient [coding wise] way to implement certain algorithms... such as traversing binary trees for example... it depends... as for coding practice, that also depends on the situation... it's kind of like goto, [but i'm more strict about using not using goto then not using recursion, personally...]

    and, salem, what about other prototypes of main? such as the
    Code:
    int main(int argc, char * argv[], char * env[])
    type? i'd imagine there are specialized startup sequences for those as well and that they'd be incompatible for recursion as well... is this in compilance with your knowledge?
    hasafraggin shizigishin oppashigger...

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    um Salems right, i don't know what compiler your using but that's very very illegal and very bad...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > and, salem, what about other prototypes of main? such as the
    envp as the 3rd parameter is a reasonably common extension (which the standard recognises as an extension).

    This prints the environment, if your compiler supports the extension
    for ( i = 0 ; envp[i] != NULL ; i++ ) printf( "%s\n", envp[i] );

    If it doesn't, it will probably blow up

    If you're desperate to call main recursively, all you need do is this
    1. rename your existing main as my_main
    2. Add this as your new main
    Code:
    int main ( int argc, char *argv[] ) {
        return my_main( argc, argv );
    }
    And the whole problem goes away. my_main is a normal C function, so you can be as recursive as you want.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    >it in fact is a very efficient [coding wise] way to implement certain algorithms

    Yes it creates very pure and elegant code, but if there is a lot of recursion nesting, it creates a lot of overhead due to the number of function calls, too.

  9. #9
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    that's a good idea salem, having a sub-main function... and i know there are environment variables for sb settings, but what others exist? / how are they practical...? thanks!
    hasafraggin shizigishin oppashigger...

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    34
    You should not call main within itself. If you are going to use recursion(it should be done within a sub function);

    one thing to remember is that if you are going to use recursion is that if you are initializing anything it will keep on doing that.

    here is a quick example of recursion

    num= the digit you want to find in the fibonacci sequence.

    int fibonacci(int num);
    {
    while (num>2)
    return(fibonacci(num-2)+fibonacci(num-1));
    if (num<=2)
    return (1);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call to realloc() inside a function: memory problem
    By simone.marras in forum C Programming
    Replies: 15
    Last Post: 11-30-2008, 09:01 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21