Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 스택의 성장 방향을 찾는 프로그램

<시간/>

스택은 요소를 저장하는 데이터 구조입니다. 스택에는 두 가지 작업이 있습니다. 스택에 새 요소를 추가하는 푸시. 스택에서 요소를 제거하는 pop.

스택은 그것을 사용하는 프로그램의 성격에 따라 위아래로 커질 수 있습니다. 프로그램에서 스택의 성장 방향을 찾는 프로그램입니다.

알고리즘

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.

예시

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

출력

Stack grows downward