1,210 views
1 votes
1 votes
Consider storage class of a variable declared as static. Where the memory will be allocated?

(A) Code Segment

(B) Stack Segment

(C) Heap Segment

(D) Data Segment

2 Answers

Best answer
2 votes
2 votes


Code segment or text segment: Code segment contains the code executable or code binary.
Data segment: Data segment is sub divided into two parts
Initialized data segment: All the global, static and constant data are stored in the data segment.
– Uninitialized data segment: All the uninitialized data are stored in BSS.
Heap: When program allocate memory at runtime using calloc and malloc function, then memory gets
allocated in heap. when some more memory need to be allocated using calloc and malloc function,
heap grows upward as shown in above diagram.
Stack: Stack is used to store your local variables and is used for passing arguments to the functions
along with the return address of the instruction which is to be executed after the function call is over.
When a new stack frame needs to be added (as a result of a newly called function), the stack grows
downward.

selected by
3 votes
3 votes

All variables that are defined inside a function are normally created on the stack each time the function is called. These variables die as soon as control goes back from the function. However if the variables inside the function are defined as static then they do not get created on the stack. Instead they are created in a place in memory called "Data Segment". Such variables die only when progran execution comes to an end.

Related questions

1 votes
1 votes
3 answers
1
jverma asked May 23, 2022
1,062 views
#include <stdio.h>int f(int n){ static int r = 0; if (n <= 0) return 1; r=n; return f(n-1) + r;}int main() { printf("output is %d", f(5)); return 0;}Ou...
0 votes
0 votes
1 answer
2
srestha asked Nov 18, 2018
1,291 views
Is it static declaration or static assignment?int main() { int x=20; static int y=x; if(x==y) printf("Equal"); else printf("Not Equal"); return 0; }What is output?and why...
2 votes
2 votes
1 answer
4