Execution Process of a Program

Thread Starter


Joined Mar 22, 2019
We use the following steps to create and execute C programs in Windows OS…

1. We write a code​
2. We Compile a code​
3. And Run Executable code​
Step 1 : We write code on compiler​
#include<stdio.h>   /* header file */

void function (void);  /* declare prototype function  */

int main (void)      /* Program start from here */
  int X = 5;        /* assign 5 to integer variable X */

  printf ( "\n X : %d", X);   /* Print value stored in X */
  function();   /* call function */
    return 0;

void function (void)   /* Return type : nothing, argument : doesn't pass any parameter */

    int Y = 12;       /* assign 12 to integer variable X */
    Y++;              /* increment Value of Y by one */ 
    printf ("\n Y : %d", Y); /* Print value stored in Y */
Step 2 : We compile code on compiler

...Program finished with exit code 0

Step 3 : Run executable file
X : 5
Y : 13

Following questions raise in my mind
  1. How does program work during execution time.
  2. Where does variable's X and Y stored in memory during the execution time.
  3. How the programmer decide whether variable should be store into heap or stack.
  4. When does compiler allocates memory for variable at compile time or run time

Thread Starter


Joined Mar 22, 2019
Do some reading and learning about programming in assembly. Along the way you will find answers to most of your questions.
I don't want to go in details I just want understand only one doubt about variables When they create and destroy in the function

#include <stdio.h>

int add(int x, int y)  // add (1, 1)
    int fresult;
      x = 3;
      y = 4;
    fresult = x + y;

int main()
    int Mresult;   
    int X = 1;
    int Y = 1;
    add(X, Y);  // when we call the function, value of X, Y will replace value of x and y  add (1, 1)

    Mresult = add (X, Y);
    printf(" Main result = %d \n",Mresult);
    return 0;
There are four variables X, Y, x and y
What happen when we call the function add(x, y) form the main function

I think value of X, Y will replace value of x and y add (1, 1)

When does X, Y, x and y creates and destroy in code ?


Joined Oct 2, 2009
What is the difference between the stack and the heap?
To answer this you need to understand the differences in embedded systems.

You can create an embedded system around an operating system (OS) such as Windows, Linux, Real Time Operating System (RTOS) and on hardware platforms such as a PC, raspberryPi, iPhone, Android smart phone, etc. In such cases, the OS is designed to run multiple applications at the same time.

You can also create an embedded system using a minimal tiny microcontroller unit (MCU) such as Microchip PIC or Atmel AVR, or very complex single chip MCUs, with or without an OS.

Multi-tasking OS manages memory usage across all applications. Before an application is launched a portion of system memory is allocated to the application and that is called the heap. Run-time space (such as the stack) required by the program is allocated from the heap.

In simple MCU embedded applications, with or without an OS, all of system memory is the heap. In most cases, only one application is ever launched and therefore the application has access to all of system memory, storage for variables usually starting from the lowest memory address and increasing. The stack is created within the system memory, usually starting from the highest memory address and decreasing. (Hence, if you think about it, there is the danger that variable memory space can collide with stack space resulting in system failure!)

Next, you need to know the difference between a local variable and a global variable.
A global variable is one that is accessible from any part of the program. To implement this on an embedded MCU, a global variable is allocated space at a fixed memory address. This is done at compile and link time by the compiler. Global variables are define outside of any function definitions in C programming language, i.e. no function "owns" the variable.

Local variables. are defined in the definition of a function. The variable is allocated space on the stack. The function "owns" the variable. No other function has access to the variable. The stack is dynamic and will grow or shrink according to the needs of each function being called. Functions can be nested, i.e. one function may call another function. When a function is called, the stack will grow. When a program exists a function, the space on the stack that was used by the function is released, i.e. the stack gets smaller. Hence the local variable no longer exists when the function is ended.
Last edited:


Joined Feb 9, 2013
A good compiler will reduce your code to this:
int z; // some unknown value from stack memory
printf(" Main result = %d \n",z);
x, y, X, and Y are never created or allocated and add() is never called. Even if you added a return statement to add(), the compiler should eliminate it:
printf(" Main result = 7 \n");
Last edited:


Joined Jun 14, 2018
  1. How does program work during execution time.

  1. You can take a look at
    for a low level example.
  2. [*]Where does variable's X and Y stored in memory during the execution time.
    [*]How the programmer decide whether variable should be store into heap or stack.
  3. That depends on the CPU/MCU and of the compiler (which can do a lot of optimization and in-lining; in your case, you may end-up with "no" variable at all, with some compiler). As for WHERE it is, it can be a fixed memory address (like a PORT, or some reserved range of memory for a display, as example), the stack of the thread of execution (single thread, or multi-thread) if the variable "space" is fixed and small (generally, and its size won't change if the data it holds is modified), or the heap if its size is variable, or huge, or... as it is convenient for the compiler.

    [*]When does compiler allocates memory for variable at compile time or run time
That also depends on the compiler and the CPU/MCU... and of the data that is hold by the "variable" (structure, array, or even "object", in C++ parlance). As example, in F#, the compiler does NOT reassign a variable, it creates a new one (which will be hiding the old one, so, effectively, "erasing" the old one), and so a new "place" in memory will be used, each time you make a modification:
(pseudo code)
x = 4;
x = x + 1;
(/pseudo code)
the resulting x, in the second line, will be stored somewhere ELSE than the x of the first line.
A more interesting question to the OP would be "why"? Why do you want/need to know?
This is not a flippant response!
You don't need to know (in the main) since unless you are programming in assembler, you are working in one or more layers of abstraction above the hardware. So for 5 9's of the time, we don't care! One of the benefits/reasons for working at a high level! Normally you give the compiler an idea about the hardware, and someone else (in the form of their algorithms) takes care of what goes where!
If you really want to wring the last ounce out of your system (or just for fun), then you are likely to want to use assembler, you are now the Mighty Smiter! You put your variables where you like, overwrite them, keep them forever, scatter them like confetti, do as you will!
If it's just for knowing, more or less, how and why a particular compiler/interpreter handles memory space, then I'd start googling and read manuals.
Have fun!
PS, if you want to play with assembler, I can recommend the PIC environment. Fairly small instruction set and a massive range of processors! the 8pin 6PIO chips are great fun and will stretch you as far as you want to go! The world is a different place when you use assembler. Sounds like a tag line! :)

Seeing the post above reminded me of this You will learn a great deal here!
I've recreated it using bluepills rather than discrete components. Creating a primitive 8 bit CPU from sophisticated MPUs appealed to my sense of the absurd!
Last edited: