GeeksforGeeks » Interview Questions
Stack is growing upwards or downwards?
(4 posts)-
To check whether processor stack is growing upwards or downwards !!!
-
Try noting down the address of a local variable. Call another function with a local
variable declared in it and check the address of that local variable and compare!.#include <stdio.h> #include <stdlib.h> void stack(int *local1); int main() { int local1; stack(&local1); exit(0); } void stack(int *local1) { int local2; printf("\nAddress of first local : [%u]", local1); printf("\nAddress of second local : [%u]", &local2); if(local1 < &local2) { printf("\nStack is growing downwards.\n"); } else { printf("\nStack is growing upwards.\n"); } printf("\n\n"); } -
int main(){ int a; int b; if(&a <&b) printf("growing up"); else printf("growing down"); getchar(); return 0; }ANS: growing down
-
void Up_down(int i) { int ptr=&i; printf("%u \n",ptr); up_down(j); } int main() { Up_down(10); getchar(); return 0; }so By looking at the address of ptr u can say whether stack is growing up or down
Reply
You must log in to post.