C Variables, Memory Model, Scope & Constants

โšก C (C17 / C23 Standard) ๐ŸŸข Lesson 2 ๐Ÿ“‚ Phase 02: Variables & Data Types ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this in-depth guide: Variables ante enti? ยท RAM Memory Model ยท Declaration, Initialization & Assignment ยท Naming Rules ยท Local vs Global Scope ยท Stack Lifetime ยท const vs #define

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.

1Variables Ante Enti? RAM Memory Architecture & Addresses

Variable ante computer RAM (Random Access Memory) lo data ni store cheyyadaniki allocate chesina Named Memory Location. Prati variable ki 3 main attributes untayi:

  1. Name (Identifier): Manam code lo refer chese peru (e.g. age).
  2. Type & Size: Variable lo store chese data type (e.g. int = 4 bytes in RAM).
  3. Memory Address: RAM lo aa variable store ayina physical hexadecimal byte address (e.g. 0x7ffee4b1), dheenni C lo &variable operator tho access cheyyavachu.
RAM Memory Model โ€” How Variables Live in RAM:
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
2Declaration, Initialization & Assignment Lifecycle

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;.

3Variable Naming Rules (Identifiers) in C

C language identifiers create cheyyadaniki strict compiler rules unnayi:

RuleValid ExampleInvalid 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 underscoresint 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 nameint student_roll_no;int student roll no; (Syntax Error!)
4Local vs Global Variables: Scope & Lifetime Architecture

C lo variable ekkada declare chesamu anedhi daani Scope (Accessibility) mariyu Lifetime (Existence in RAM) ni decide chesthundhi:

AttributeLocal 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 ValueGarbage Value (Unpredictable junk!)Zero (0) automatically initialized by runtime.
Lifetime in RAMCreated when function is called; destroyed when function returns!Created when program starts; persists until program exits!
C โ€” Local vs Global Scope Demo โ–ถ Run Code
#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;
}
5Constants in C: const Keyword vs #define Preprocessor Macros

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!

C โ€” Constants in Action โ–ถ Run Code
#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;
}
๐Ÿ’ป Try It Yourself โ€” Test Variables & Memory in C Compiler

Run this variable inspection and memory address print program:

C (GCC Standard) โ–ถ Open C Compiler
#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;
}
Open in Online C Compiler โ†’