C Variables, Memory Model, Scope & Constants
Welcome to Phase 2 (Part 1): C Variables, Memory Model, Scope & Constants! In C, variables are not abstract concepts โ they directly represent physical chunks of bytes allocated in your computer's RAM memory. Understanding how C manages memory allocation, stack frames, variable lifetimes, and immutable constants is the foundational secret to mastering low-level programming and pointers. In this in-depth guide, you will master the variable lifecycle, memory addressing with the address-of operator (&), local vs global storage segments, and the crucial differences between const and #define.
Variable ante computer RAM (Random Access Memory) lo data ni store cheyyadaniki allocate chesina Named Memory Location. Prati variable ki 3 main attributes untayi:
- Name (Identifier): Manam code lo refer chese peru (e.g.
age). - Type & Size: Variable lo store chese data type (e.g.
int= 4 bytes in RAM). - Memory Address: RAM lo aa variable store ayina physical hexadecimal byte address (e.g.
0x7ffee4b1), dheenni C lo&variableoperator tho access cheyyavachu.
int age = 21; (Takes 4 Bytes on Stack Memory)
RAM Address: 0x1000 0x1001 0x1002 0x1003
โโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโ
Byte Data (Hex): โ 0x15 โ 0x00 โ 0x00 โ 0x00 โ = 21 in Decimal
โโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโดโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโ
Variable Name: "age"
Address (&age): 0x1000
C language lo variable creation 3 distinct stages lo untundhi:
1. Declaration:
Compiler ki variable peru mariyu data type cheppadam. Ee stage lo memory allocate avthundhi kaani value pettamu: int score;
โ ๏ธ Garbage Value Warning: C lo declare chesi initialize cheyyani local variables lo RAM lo mundhe unna random junk data (Garbage Value) untundhi!
2. Initialization:
Variable ni declare chesthune first time value assign cheyyadam: int score = 100; (Memory allocation + Initial value stored at the same instant).
3. Assignment:
Already declare ayina variable lo unna value ni kotha value tho overwrite cheyyadam: score = 250;.
C language identifiers create cheyyadaniki strict compiler rules unnayi:
| Rule | Valid Example | Invalid Example (Compiler Error) |
|---|---|---|
Must start with Alphabet (a-z, A-Z) or Underscore (_) | int age;, int _count; | int 2age; (Cannot start with a digit!) |
| Can contain letters, digits, and underscores | int total_marks1; | int total-marks;, int total$ (No special chars!) |
Case Sensitive (age, Age, AGE are 3 different variables!) | int age = 10; int Age = 20; | Accidental case mismatch causes undeclared identifier. |
| Cannot use C Reserved Keywords (32 Keywords) | int my_return; | int return;, int while;, int int; (Keyword error!) |
| No Whitespace / Spaces allowed inside variable name | int student_roll_no; | int student roll no; (Syntax Error!) |
C lo variable ekkada declare chesamu anedhi daani Scope (Accessibility) mariyu Lifetime (Existence in RAM) ni decide chesthundhi:
| Attribute | Local Variable (Stack Memory) | Global Variable (Data Segment) |
|---|---|---|
| Where Declared? | Inside a function or code block { ... }. | Outside all functions (at top of file). |
| Scope (Visibility) | Only inside the specific function/block where declared. | Accessible throughout the entire program. |
| Default Value | Garbage Value (Unpredictable junk!) | Zero (0) automatically initialized by runtime. |
| Lifetime in RAM | Created when function is called; destroyed when function returns! | Created when program starts; persists until program exits! |
#include <stdio.h>
// 1. Global Variable (Stored in Data Segment, lives entire program)
int globalCounter = 500;
void testFunction(void) {
// 2. Local Variable (Created in Stack frame of testFunction)
int localVal = 10;
printf("Inside testFunction: localVal = %d, globalCounter = %d\n", localVal, globalCounter);
} // localVal is destroyed from Stack here!
int main(void) {
int mainLocal = 100;
testFunction();
printf("Inside main: mainLocal = %d, globalCounter = %d\n", mainLocal, globalCounter);
// printf("%d", localVal); // โ Compile Error: 'localVal' is undeclared in main scope!
return 0;
}
Program execution lo value eppudu change avvakunda Read-Only ga unchalante Constants vadathamu. C lo 2 ways unnayi:
1. const Keyword (Type-Safe Compiler Constant)
const double PI = 3.14159;
โข Proper data type untundhi (compiler type-checking chesthundi).
โข Reassign cheyyadaniki try chesthe compiler error: assignment of read-only variable throw chesthundi.
2. #define Preprocessor Macro (Text Replacement)
#define MAX_USERS 100
โข Preprocessor stage loni text substitution jaruguthundhi (Zero memory allocated in binary).
โข No semicolon ; at the end!
#include <stdio.h>
#define APP_VERSION 2.5 // Preprocessor Constant
const int MAX_LIMIT = 500; // Type-Safe const Variable
int main(void) {
printf("Application Version: %.1f\n", APP_VERSION);
printf("Maximum Limit: %d\n", MAX_LIMIT);
// MAX_LIMIT = 600; // โ Compile Error: Assignment of read-only variable!
return 0;
}
Run this variable inspection and memory address print program:
#include <stdio.h>
int main(void) {
int score = 95;
printf("Score value: %d\n", score);
printf("RAM Address of score (&score): %p\n", (void*)&score);
return 0;
}