C Program to find direction of growth of stack


A stack is a data structure that stores elements. There are two operations on the stack. push that adds a new element to the stack. pop that removes an element from the stack.

The stack can grow upward and downward according to the nature of the program that uses it. The program to find the direction of the growth of stack in a program.

Algorithm

Step 1: Create a local variable in the main function.
Step 2: Create a function that with a local variable.
Step 3: Call the function from the main function. And then compare the local variables of in both these functions.
Step 4: Compare the address of local variables in main and the function.
Step 5: If address variable in main is more than local variable of the function, then stack grows upward otherwise it grows downward.

Example

 Live Demo

#include<stdio.h>
void fun(int *main_local_addr){
   int fun_local;
   if (main_local_addr < &fun_local)
      printf("Stack grows upward
");    else       printf("Stack grows downward
"); } int main(){    int main_local;    fun(&main_local);    return 0; }

Output

Stack grows downward

Updated on: 19-Sep-2019

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements