Hello everyone,in the previous notes ,i just tell you about data structures and algorithms , terminologies .Now i will tell you all about memory layout of C programs .SO LET’S GET STARTED:

The memory layout of C programs is given below:-


Representation of C program consists of following segments:-

  1. Code segment(TEXT SEGMENT)
  2. Initialized data segment
  3. Uninitialized data segment
  4. Stack
  5. Heap

For now, focus on first section i.e code segment

When the program starts, its code is copied to the main memory


  • A code segment , also known as a text segment
  • It is one of the sections of a program in main memory i.e RAM, which contains executable instructions.
  • As a memory region, a code segment may be placed below the heap and stack why?

REASON:-In order to prevent heaps and stack overflows from overwriting it.

  • NOTE:-
  • The code segment is sharable so that only a single copy needs to be in memory for frequently executed programs, as text editors, the C compiler etc.

*Initialized data , usually called Data Segment.

*A data segment is a part of virtual address space of a program, which contains the (global variables + static variables)that are initialized by the programmer/users.

  • Data segment is not read-only(i.e a permission to access files where the user is only read or see or you can say view it ,not to make changes), since the values of the variables can be altered at run time.
  • This segment can be further classified into 2 parts:-
  • read-only area
  • read-write area.

For instance,

  • The global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area.
  • And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.

Ex: static int i = 5 will be stored in data segment and global int i = 5will also be stored in data segment.


  • Called the “bss” segment, named after an ancient assembler operator that stood for “block started by symbol”
  • Data in this segment is initialized to arithmetic 0 before the program starts executing.
  • Uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to 0 or do not have explicit initialization in source code.
#include <stdio.h>

char c; /* Uninitialized variable stored in bss*/

int main()
static int i; /* Uninitialized static variable stored in bss */
return 0;

NOTE:- Initialized and Uninitialized data segments hold Initialized and Uninitialized global variables respectively.


Stack holds the memory occupied by the functions.

Stack segment is used to store all 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.

Local variables have a scope to the block which they are defined in, they are created when control enters into the block.

All recursive function calls are added to stack.


  • Heap contains the data which is requested by the program as dynamic memory.
  • Heap is the segment where (It is when an executing program requests that the operating system give it a block of main memory) usually takes place.
  • When some more memory need to be allocated using malloc and calloc function, heap grows upward.
  • The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
#include <stdio.h>
int main()
{ /*memory allocating heap segment*/
char *p=(char*)malloc(sizeof(char));
return 0;

So, these are the explanation of all the segments of c programs. Hope u all like it.I will be covering Time Complexity and Big O in my next blog ,so keep in touch with me.



No one is born as a writer .You must become a writer because you never stop learning:-).