| Category / Type | Declaration Example | Scope | Lifetime | Memory Location | Default Initialization | Working / Behavior |
|---|---|---|---|---|---|---|
| Global Variable | int g = 10; |
Visible to all functions after declaration | Until program ends | Data Segment (Initialized Data) | 0 if uninitialized | Accessible by any function. Stored once globally. |
| Static Global Variable | static int g = 5; |
File scope only (not visible outside file) | Until program ends | Data Segment | 0 if uninitialized | Retains value; can’t be accessed by other files. |
| Local (Auto) Variable | void func(){ int a = 10; } |
Inside the function/block | Until function exits | Stack | Garbage value | Created each time function is called, destroyed when it returns. |
| Static Local Variable | void func(){ static int c = 0; } |
Visible only inside that function | Until program ends | Data Segment | 0 if uninitialized | Remembers its last value between function calls. |
| Extern Variable | extern int g; |
Global across files | Until program ends | Data Segment | Depends on actual definition | Declares external variable (defined elsewhere). No memory allocated here. |
| Register Variable | register int x = 10; |
Function/block | Until function exits | CPU Register (if available) | Garbage value | Hint to compiler; may use a register for faster access. |
| Volatile Global Variable | volatile int sensorValue; |
Global | Until program ends | Data Segment | 0 if uninitialized | May change outside program control; prevents compiler optimizations. |
| Volatile Local Variable | void f(){ volatile int flag; } |
Local to function | Until function exits | Stack | Garbage value | Each read re-fetches from memory; compiler won't cache value. |
| Const Variable | const int x = 100; |
Depends on location | Depends on location | .rodata (Read-only Data) if global/static | Must be initialized | Read-only; can't be modified after initialization. |
| Dynamic (malloc) | int *p = malloc(10*sizeof(int)); |
Accessible through pointer | Until free() called |
Heap | Indeterminate | Allocated at runtime; must be freed manually. |
| Dynamic (calloc) | int *p = calloc(10,sizeof(int)); |
Accessible through pointer | Until free() called |
Heap | All elements = 0 | Like malloc but zero-initialized. |
| Dynamic (realloc) | p = realloc(p, 20*sizeof(int)); |
Accessible through pointer | Until free() called |
Heap | Preserves existing data | Resizes allocation; block may move to a new address. |
--------------------------------------------------------------------------
| +-----------------------------+ |
| | STACK | ← grows downwards |
| | - Local variables | |
| | - Function call frames | |
| +-----------------------------+ |
| | HEAP | ← grows upwards |
| | - malloc/calloc/realloc | |
| +-----------------------------+ |
| | BSS Segment | |
| | - Uninitialized globals | |
| | - Uninitialized static vars| |
| +-----------------------------+ |
| | DATA Segment | |
| | - Initialized globals | |
| | - Initialized static vars | |
| +-----------------------------+ |
| | TEXT / CODE | |
| | - Function code, constants | |
| +-----------------------------+ |
--------------------------------------------------------------------------
| Concept | Stack | Heap | Data Segment (Static/Global) |
|---|---|---|---|
| Allocation | Automatically by compiler | Manually via malloc/calloc | At program load time |
| Deallocation | Automatic on return | manual (free()) | Automatic at program end |
| Lifetime | Function call duration | Until freed | Entire program |
| Speed | Very fast | Slower | Fast |
| Typical Use | Local vars, function params | Dynamic data, linked lists | Globals, statics |
The buttons above let you copy or download the same table as CSV. This is client-side JS only (no server required).