Q46: Where is this variable stored?

Based on the following memory layout, answer the following question.

memory layout

Consider the following C program:

#include <stdio.h>

int iglobal = 10;

int uglobal;

&nbsp;

int main(void)

{

static int i;

int j;

return 0;

}

Question #46: Where does the variable ‘j’ go?

Options:

  1. heap
  2. Uninitialized data (bss)
  3. Stack
  4. Initialized data

Solution: ‘j’ is a local variable, all local variables go to stack. Hence, the correct answer is option 3. Checkout a similar question here.

Q14: Memory layout of C programs

Following is the memory layout of a typical C program:

pic1

Now, consider the C program below:

#include <stdio.h>

int iglobal = 10;
int uglobal;

int main(void)
{

  static int i;
  int j;

  return 0;
}

Question #14: Where does the variable i go in memory?

Options:

A)     Heap

B)      Uninitialized data (bss)

C)      Stack

D)     Initialized data

Solution: All static variables have the scope of the lifetime of the program, so they have to go to data segment. Since it is uninitialized, it goes to bss. So option B is the correct one.