Thread: Explain me how the stack looks like for any C program

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Post Explain me how the stack looks like for any C program

    how the stack would look like for the following program if I give input as 5.


    Code:
    #include <stdio.h>
    
    int fibonacci(int number)
    {
      int retval;
      if (0 == number){
        return 0;
      }
      if (1 == number){
        return 1;
      }
      return(fibonacci(number-1) + fibonacci(number-2));
    }
    
    int main()
    {
      int number = 0;
      int fibvalue = 1;
      while (1){
        printf("please enter the number\n");
        scanf("%d", &number);
        fibvalue = fibonacci(number);
        printf("computed fibonacci value %d\n", fibvalue);
      }
      return 1;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Depends on the microarchitecture and the direction of stack push/pop.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The stack will contain return addresses (where the function was called from) as well as any local variables. It is dynamic, so I'm not sure exactly when you'd want to take a snapshot of its contents. I assume the stack will be at its deepest during the fibonacci (number -1) call.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-14-2009, 07:45 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Stringy Sums
    By bumfluff in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2006, 01:52 AM
  5. HEap and stack, I'm confused
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-31-2002, 10:59 AM