When a variable is declared compiler automatically allocates memory for it. This is known as compile time memory allocation or static memory allocation. Memory can be allocated for data variables after the program begins execution.
How are variables allocated memory in C?
The C language supports two kinds of memory allocation through the variables in C programs:
- Static allocation is what happens when you declare a static or global variable. ...
- Automatic allocation happens when you declare an automatic variable, such as a function argument or a local variable.
When should you allocate memory in C?
Dynamic allocation is required when you don't know the worst case requirements for memory. Then, it is impossible to statically allocate the necessary memory, because you don't know how much you will need. Even if you know the worst case requirements, it may still be desirable to use dynamic memory allocation.