⚙️ C Programming Masterclass Notes
1️⃣ Introduction to C & The 4-Stage Compilation Pipeline
Definition: C ek general-purpose, procedural computer programming language hai jise 1972 me Dennis Ritchie ne Bell Labs me develop kiya tha. Isse "Mother of all modern languages" kaha jata hai kyunki modern OS (Linux, Windows kernel), databases (MySQL), aur runtimes (Python/Node) C/C++ me likhe gaye hain.
| Stage | Tool | Input → Output File | Core Responsibility |
|---|---|---|---|
| 1. Preprocessing | Preprocessor | prog.c → prog.i |
Macros replace karna (#define), header files expand karna (#include). |
| 2. Compilation | Compiler (GCC) | prog.i → prog.s |
Clean C code ko target hardware ki Assembly Language me translate karna. |
| 3. Assembly | Assembler | prog.s → prog.o |
Assembly mnemonics ko binary machine code (Object file) me convert karna. |
| 4. Linking | Linker | prog.o → a.out / prog.exe |
C Standard Library functions (printf) ko object file se bind karna. |
# Complete Compilation Command:
gcc -Wall -O2 main.c -o myprogram
./myprogram
2️⃣ Data Types, Format Specifiers & Sizes
| Data Type | Memory Size (64-bit OS) | Format Specifier | Value Range |
|---|---|---|---|
int |
4 Bytes (32 bits) | %d or %i |
-2,147,483,648 to +2,147,483,647 |
char |
1 Byte (8 bits) | %c |
-128 to 127 (or 0 to 255 unsigned) |
float |
4 Bytes | %f |
6 decimal places precision |
double |
8 Bytes | %lf |
15 decimal places precision |
pointer |
8 Bytes | %p |
Hexadecimal RAM address |
3️⃣ Control Flow & Functions
Execution order control karne ke liye branching aur looping constructs:
#include <stdio.h>
// Function Declaration (Prototype)
int calculateFactorial(int n);
int main() {
int num = 5;
printf("Factorial of %d is: %d\n", num, calculateFactorial(num));
return 0;
}
// Function Definition
int calculateFactorial(int n) {
if (n <= 1) return 1;
return n * calculateFactorial(n - 1); // Recursion
}
4️⃣ Memory Layout of a C Program
Jab koi compiled C binary RAM me load hoti hai, toh wo 5 distinct segments me divide hoti hai:
| Memory Segment | Lifetime | Managed By | Stored Data |
|---|---|---|---|
| Stack | Function call end hone tak | CPU automatically | Local variables, parameters, return addresses (Fast). |
| Heap | Manual allocation / free | Programmer manually | Dynamic memory (malloc, calloc) (Grows upwards). |
| BSS Segment | Program execution tak | OS Loader | Uninitialized global aur static variables (Zero-filled). |
| Data Segment | Program execution tak | OS Loader | Explicitly initialized global & static variables. |
| Text / Code | Program execution tak | Read-Only Memory | Compiled binary CPU machine instructions. |
5️⃣ Pointers & Memory Address Dereferencing
Pointer: Ek aisa variable jo kisi doosre variable ka physical hexadecimal RAM address hold karta hai.
&(Address-Of Operator): Variable ka RAM address return karta hai.*(Dereference Operator): Pointer jis address ko point kar raha hai, us address par rakhi actual value return ya update karta hai.
int x = 25;
int *ptr = &x; // ptr holds the memory address of x (e.g. 0x7ffd9a)
printf("Value of x: %d\n", x); // 25
printf("Address of x: %p\n", &x); // 0x7ffd9a
printf("Value via ptr: %d\n", *ptr); // 25 (Dereferenced)
*ptr = 100; // Mutates original x directly in RAM!
printf("New value of x: %d\n", x); // 100
6️⃣ Dynamic Memory Allocation (DMA)
Runtime par heap memory allocate aur deallocate karne ke 4 core functions (<stdlib.h>):
| Function | Syntax | Behavior |
|---|---|---|
malloc() |
ptr = (int*) malloc(5 * sizeof(int)); |
Heap me raw bytes allocate karta hai (Contains garbage values). |
calloc() |
ptr = (int*) calloc(5, sizeof(int)); |
Memory allocate karke saare bytes ko 0 se initialize karta hai. |
realloc() |
ptr = realloc(ptr, 10 * sizeof(int)); |
Existing allocated memory block ka size expand ya shrink karta hai. |
free() |
free(ptr); ptr = NULL; |
Heap memory release karta hai (Memory Leak prevent karne ke liye). |
free(ptr) kar dete hain, par pointer ko NULL assign nahi karte, toh wo ek invalid deallocated memory ko point karta rehta hai (Dangling Pointer). Isse program crash ho sakta hai.
7️⃣ User-Defined Types: struct vs union
| Criteria | struct |
union |
|---|---|---|
| Memory Allocation | Har member ke liye dedicated alag memory (Sum of all members). | Sabhi members ek hi common memory space share karte hain (Size of largest member). |
| Accessing Members | Sabhi members ko ek sath simultaneously access kiya ja sakta hai. | Ek time par sirf ek hi member valid value hold kar sakta hai. |
// Typedef Struct Definition
typedef struct {
int id;
char name[50];
float gpa;
} Student;
int main() {
Student s1 = {101, "Rahul Sharma", 8.95};
printf("ID: %d | Name: %s | GPA: %.2f\n", s1.id, s1.name, s1.gpa);
return 0;
}