Thread: Recursive example

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Recursive example

    This was given as an example of recursivity. It prints 123456789987654321.

    Code:
    #include <stdio.h>
    
    void printnum ( int begin )
    {
        printf( "%d", begin );
        if ( begin < 9 )         /* The base case is when begin is no longer */
        {                           /* less than 9 */
            printnum ( begin + 1 );
        }
        /* display begin again after we've already printed everything from 1 to 9
         * and from 9 to begin + 1 */
        printf( "%d", begin );
    }
    
    int main() {
        printnum(1);
    }
    Now, I can see this code printing 1234567899, but whence does it count back down? I don't see where the code goes back and counts down.

    And, I know this has nothing to do with that, but it's easier than two threads. Is it possible to write a program that will open another progam? Such as, a program that will accept integers to select from a menu of programs, like notepad = 1, and then run that program's executable, and close itself?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by CodeGeek20 View Post

    Now, I can see this code printing 1234567899, but whence does it count back down? I don't see where the code goes back and counts down.
    The easiest way to see what is going on is to run your program through a debugger.

    What's happening is the call to printnum(9) is called from printnum(8). So when it returns, it returns back to printnum(8), which continues to execute it's last line, before returning to printnum(7). This goes on untill printnum(1), which returns to main, and ends the program.

    And, I know this has nothing to do with that, but it's easier than two threads. Is it possible to write a program that will open another progam? Such as, a program that will accept integers to select from a menu of programs, like notepad = 1, and then run that program's executable, and close itself?
    Yes, but it'd be platform specific.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Also, it's not very safe. If someone moves or renames notepad, or worse, replaces it with another executable, your program breaks.
    Why do you want to do that? If you want to make a task launcher, fine, but otherwise there is probably a better way.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. difference between recursive and iterative
    By Micko in forum C Programming
    Replies: 33
    Last Post: 07-06-2004, 09:34 PM
  3. Algorithm help (Changing from Recursive to Non Recursive)
    By Thantos in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2004, 07:27 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM