Har topic ke teen hisse hain โ ye kya hai (easy words mein), ek real life ka example jisse concept turant samajh aaye, aur ek real C program jisse code dekh kar practice ho sake.
Unit 1Introduction to C
1.1 โ Features & Applications of C
C Language Kya Hai?
C ek programming language hai โ matlab computer ko instructions dene ka ek tarika, jisse hum use bata sakte hain kya karna hai. Ise 1972 mein Dennis Ritchie ne banaya tha. C ko "mother of all languages" bhi kaha jata hai kyunki Python, Java jaisi bahut si languages isi se inspire hoke banayi gayi hain.
๐ Real Life Example
C ko ek bhasha (language) ki tarah socho jisse tum ek carpenter (mistri) ko ghar banane ki exact instructions dete ho โ "pehle neev khodo, fir deewar banao, fir chhat dalo." Computer bhi C ke through exact step-by-step instruction samajhta hai.
Kyun Padhna Zaroori Hai
C seekhne se computer andar se kaise kaam karta hai ye samajh aata hai โ memory, logic, aur structure. Isliye ise foundation language kaha jata hai; ek baar C aa gaya to koi bhi language seekhna easy ho jata hai.
Feature
Simple Matlab
Kaam Kyun Aata Hai
Fast & Efficient
Seedha machine-understandable code banta hai
Program turant, bina delay chalta hai
Portable
Ek computer ka code dusre computer par bhi chal sakta hai
Har jagah same code reuse ho sakta hai
Structured
Code chhote-chhote functions mein bat jata hai
Samajhna aur sudharna aasan ho jata hai
Rich Library
Bahut se ready-made tools milte hain
Khud se sab kuch banane ki zarurat nahi
Kahan use hoti hai: Operating systems (Windows/Linux ka core), microwave/AC jaise embedded chips, game engines, database systems, aur competitive programming mein โ kyunki ye sabse fast languages mein se ek hai.
1.2 โ Structure of a C Program
Program Ka Fixed Pattern
Har C program ek jaise pattern mein likha jata hai โ jaise ek letter likhne ka bhi ek format hota hai (Date, To, Subject, Body, Signature), waise hi C program ka bhi ek format hota hai.
๐ Real Life Example
Ek recipe card socho: sabse upar ingredients ki list (header files), phir steps (main function ke andar statements), aur last mein "serve karo" (return statement). Bina kisi step ko follow kiye recipe pura nahi hoti โ program bhi waise hi hai.
#include <stdio.h> // Ingredients: built-in tools mangwao
int main() { // Recipe yahan se shuru hoti hai
int a = 5, b = 10;
int sum = a + b;
printf("Sum = %d", sum); // Final dish serve karo
return 0; // Bata do ki sab sahi hua
}
Kyun Zaroori Haimain() function hamesha compulsory hota hai kyunki computer program ko run karte waqt sabse pehle yahin se shuru karta hai โ chahe baaki functions kahin bhi likhe ho.
Header Files
Header File Kya Hoti Hai?
Header file ek aisi file hai jisme pehle se hi kuch functions ready-made likhe hote hain, jinhe hum apni marzi se apne program mein mangwa (include) sakte hain.
๐ Real Life Example
Ye bilkul aisa hai jaise tum kisi library se kitab issue karte ho instead of khud kitab likhne ke. #include <stdio.h> likhna matlab "mujhe wo library de do jisme printf aur scanf jaise tools already likhe hain."
#include <stdio.h> // printf, scanf ke liye
#include <math.h> // sqrt(), pow() jaise math tools ke liye
#include <string.h> // strlen(), strcpy() jaise string tools ke liye
Kyun Use Karte Hain
Agar header file na ho to hume printf jaisa simple function bhi khud banana padega โ jo time-waste hai. Header file se hume ye "already bana bana kaam" free mein mil jata hai.
Tokens
Token Kya Hota Hai?
Token program ka sabse chhota "meaningful" tukda hota hai โ jaise ek sentence alag-alag words se milkar banta hai, waise C program bhi tokens se milkar banta hai.
๐ Real Life Example
Ek sentence socho โ "Ram ne aam khaya". Isme "Ram", "ne", "aam", "khaya" alag-alag words hain jo milkar meaning banate hain. C program mein bhi int, age, =, 25 alag tokens hain.
int age = 25;
Token
Type
Matlab
int
Keyword
Fixed meaning wala reserved word
age
Identifier
Humne diya hua naam
=
Operator
Value assign karne ka symbol
25
Constant
Fixed value
Kyun Zaroori Hai
Compiler program ko tokens mein todkar hi padhta hai โ jaise hum ek sentence ko word-by-word padhte hain.
Comments
Comment Kya Hota Hai?
Comment wo line hai jise compiler bilkul ignore kar deta hai โ ye sirf insaan (programmer) ke padhne/samajhne ke liye hoti hai.
๐ Real Life Example
Bilkul sticky notes jaisa โ jab tum kisi kitab mein margin par apne liye note likhte ho ("ye important hai"), wo note kitab ka content nahi hota, sirf tumhari madad ke liye hota hai.
// Ye single-line comment hai/* Ye multi-line comment hai
Jo kai lines tak ja sakta hai */
int main() {
int a = 5; // a mein 5 store hai
return 0;
}
Data Types
Data Type Kya Hota Hai?
Data type batata hai ki ek variable mein kaisa data (number, decimal, letter) store hoga aur usko rakhne ke liye kitni jagah (memory) chahiye.
๐ Real Life Example
Ye bilkul different size ke containers jaisa hai โ chawal rakhne ke liye bada dabba chahiye, masala rakhne ke liye chhota dabba. Waise hi, ek poora number rakhna hai to int use karo, decimal number rakhna hai to float, aur ek single letter rakhna hai to char.
Data Type
Kab Use Karein
Example
int
Poore number (bina decimal ke)
int age = 20;
float
Decimal number, kam precision
float price = 99.5;
double
Decimal number, zyada precision
double pi = 3.14159265;
char
Ek single character
char grade = 'A';
int main() {
int marks = 90;
float percentage = 85.5;
char grade = 'A';
printf("Marks: %d, Percentage: %f, Grade: %c", marks, percentage, grade);
return 0;
}
Format Specifiers
Format Specifier Kya Hai?
Ye ek chhota sa code (%d, %f, etc.) hai jo printf aur scanf ko batata hai ki kis "type" ka data print ya read karna hai.
๐ Real Life Example
Ye courier ke parcel label jaisa hai โ label pe likha hota hai "Fragile" ya "Liquid" taaki delivery boy ko pata ho parcel ke andar kya hai aur usko kaise handle karna hai. Waise hi %d bata deta hai "andar integer hai."
Specifier
Data Type
%d
int
%f
float
%lf
double
%c
char
%s
string
int a = 10;
char c = 'X';
printf("Value = %d, Char = %c", a, c);
scanf("%d", &a); // &a matlab a ka memory address
Constants and Variables
Constant vs Variable
Variable ek naam hai jisme rakhi value badli ja sakti hai. Constant ek fixed value hai jo program ke chalte waqt kabhi nahi badalti.
๐ Real Life Example
Tumhari date of birth ek constant hai โ ye kabhi change nahi hoti. Lekin tumhari age ek variable hai โ har saal badalti rehti hai. Programming mein bhi aise hi kaam karta hai.
#include <stdio.h>
#define PI 3.14159 // constant define karne ka tarika
int main() {
const int MAX_AGE = 100; // ye bhi constant hai
int age = 25; // variable โ badal sakta hai
age = 26; // valid
printf("PI = %f", PI);
return 0;
}
I/O Functions
Input aur Output Functions
Ye functions user se data lene (input) aur user ko data dikhane (output) ke kaam aate hain.
๐ Real Life Example
Ek ATM machine jaisa socho โ jab tum apna PIN daalte ho, wo input hai (scanf). Jab machine screen pe "Cash Withdrawn" dikhati hai, wo output hai (printf).
#include <stdio.h>
int main() {
int age;
printf("Enter your age: "); // output
scanf("%d", &age); // input
printf("Your age is %d", age);
return 0;
}
Operators
Operator Kya Hota Hai?
Operators wo symbols hain jo values par koi operation (jaise jodna, compare karna) perform karte hain.
๐ Real Life Example
Ye ek calculator ke buttons jaise hain โ + button dabao to add hoga, - dabao to subtract. Programming mein bhi wahi symbols kaam karte hain.
Type
Operators
Kaam
Arithmetic
+ - * / %
Math calculations
Relational
> < == !=
Do values compare karna
Logical
&& || !
Multiple conditions jodna
Assignment
= += -=
Value assign/update karna
int a = 10, b = 3;
printf("Sum = %d\n", a + b);
printf("Remainder = %d\n", a % b); // % bacha hua part deta hai
printf("Is a > b? %d\n", a > b); // 1 = true
Expressions & Evaluation
Expression Kya Hota Hai?
Expression variables, constants aur operators ka mix hota hai jo ek final value deta hai.
๐ Real Life Example
School wala BODMAS yaad hai? 5 + 3 * 2 mein pehle multiplication hota hai, fir addition. C mein bhi expressions isi tarah evaluate hote hain.
int a = 5, b = 3, c;
c = a + b * 2; // pehle b*2 = 6, fir a+6 = 11
Type Casting
Type Casting Kya Hai?
Type casting matlab ek data type ki value ko dusre data type mein convert karna.
๐ Real Life Example
Ye currency exchange jaisa hai โ jab tum Dollars ko Rupees mein convert karwate ho airport pe, value same rehti hai lekin uska "type" (currency) change ho jata hai.
int a = 5, b = 2;
float result = (float) a / b; // (float) = type casting
printf("Result = %f", result); // Output: 2.500000
Kyun Zaroori Hai
Agar casting na karte, to 5/2 ka answer 2 hi aata (decimal cut ho jata), jo galat ho sakta hai jab humein exact decimal answer chahiye.
Precedence and Associativity
Kaunsa Operator Pehle Chalega?
Precedence batata hai kaunsa operator pehle evaluate hoga. Associativity batata hai agar precedence same ho to calculation left-se-right hogi ya right-se-left.
๐ Real Life Example
Ek recipe ke steps jaise โ agar recipe kahe "pehle onion fry karo, fir tamatar dalo," to order follow karna hi padega. Galat order se dish kharab ho sakti hai.
int result = 10 + 5 * 2; // * pehle: 10 + 10 = 20
1.3 โ Decision Control Structures
if Statement
Jab sirf ek condition check karni ho, aur wo true ho to hi kuch karna ho.
๐ Real Life Example"Agar barish ho rahi hai, to chhata lo." โ bas ek condition, aur uska ek hi outcome.
if (marks >= 40) {
printf("Pass ho gaye!");
}
if-else Statement
Condition true ho to ek kaam, false ho to doosra kaam.
๐ Real Life ExampleTraffic signal: agar light green hai to gaadi chalao, warna (else) ruko.
if (age >= 18) {
printf("Vote de sakte ho");
} else {
printf("Abhi vote nahi de sakte");
}
if-else-if Ladder
Multiple conditions ek ke baad ek check karni ho.
๐ Real Life ExampleExam grading: agar 90+ hai to A, warna agar 75+ hai to B, warna agar 50+ hai to C โ jaisa teacher marksheet banate waqt sochta hai.
if (marks >= 90) printf("Grade A");
else if (marks >= 75) printf("Grade B");
else if (marks >= 50) printf("Grade C");
else printf("Fail");
switch Statement
Ek hi variable ki value ko multiple fixed cases se compare karna.
๐ Real Life ExampleTV remote: ek number dabao (case), ussi number ka channel aa jata hai. Har number ka apna fixed outcome hai.
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
default: printf("Invalid day");
}
Nesting of Decision Structures
Ek if/switch ke andar dusra if/switch likhna.
๐ Real Life ExampleAirport security: pehle gate pe ticket check hota hai, tabhi andar jaane diya jata hai โ aur andar phir se ek aur checkpoint (bag scan) hota hai. Ek condition pass karne ke baad hi agli condition check hoti hai.
if (age >= 18) {
if (gender == 'M') {
printf("Eligible male voter");
} else {
printf("Eligible female voter");
}
} else {
printf("Not eligible");
}
๐ Real Life Example
Ye exam-hall strategy jaisa hai โ pehle wo questions solve karo jo aasan lagte hain (time bachta hai), phir mushkil wale pe time lagao. Competitive programming mein bhi yahi approach kaam aati hai.
Strategy
Simple Matlab
Brute Force
Saare possible options try karo โ simple par slow
Two Pointer
Do jagah se ek saath check karke time bachana
Divide & Conquer
Bade problem ko chhote hisso mein todo
Greedy
Har step pe jo best option dikhe wahi lo
Dynamic Programming
Ek baar solve kiye hue answer ko store kar lo, dobara solve mat karo
Beyond Syllabus โ cin, cout (C++)
Ye C++ ke concepts hain, jo competitive programming mein bahut use hote hain.
๐ Real Life Examplecin aur cout bhi ATM machine jaise hi hain (jaisa scanf/printf tha) โ bas C++ ka apna tarika hai input/output karne ka.
#include <iostream>
using namespace std; // std ke functions bina prefix ke use karne ke liye
int main() {
int age;
cout << "Enter your age: "; // output
cin >> age; // input
cout << "Your age is " << age;
return 0;
}
Term
Matlab
iostream
Header file jisme cin, cout defined hain
namespace std
Standard functions ka ek group/family
bits/stdc++.h
Ek hi header jisme C++ ki saari libraries already included hain
ยทยทยท
Unit 2Control Structure, Data Organization, Modular Programming
2.1 โ Loop Control Structures
Loop Kyun Chahiye?
Jab ek kaam ko baar-baar repeat karna ho, to loop use karte hain taaki same code baar-baar na likhna pade.
๐ Real Life ExampleWashing machine ka wash cycle โ machine ek hi action (ghumana) baar-baar repeat karti hai jab tak cycle poora na ho. Loop bhi bilkul waise hi kaam karta hai.
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
while loop
Jab condition check karni ho lekin exact count pata na ho.
๐ Example "Jab tak tanki full nahi hoti, paani bharte raho" โ pata nahi kitni baar bharna padega, bas condition check karte raho.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
do-while loop
Loop kam se kam ek baar zaroor chalega, chahe condition false ho.
๐ Example ATM pehle PIN maangta hi hai (ek baar to check karega hi), chahe pehli baar hi PIN galat kyun na ho.
int i = 10;
do {
printf("%d ", i);
i++;
} while (i <= 5);
// Output: 10 (ek baar chalega, condition false hone ke bawajood)
Nested Loop
Ek loop ke andar doosra loop.
๐ ExampleClass ki seating: rows (outer loop) aur har row mein columns/seats (inner loop) โ dono ek saath mil kar poori class cover karte hain.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j);
}
printf("\n");
}
Jumping Statements โ break, continue, goto
Statement
Kaam
Real Life Example
break
Loop turant band kar deta hai
Bus ka "stop" button dabana โ turant ruk jaati hai
continue
Current step skip karke agle pe jaata hai
Gaane playlist mein "skip song" dabana
goto
Seedha kisi labeled line pe jump karta hai
Kitab mein "page 50 pe jao" jaisa direct jump (kam use hota hai)
for (int i = 1; i <= 10; i++) {
if (i == 5) break; // 5 pe loop band
printf("%d ", i);
}
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // 3 ko skip karega
printf("%d ", i);
}
2.2 โ Array & String
Array Kya Hai?
Array ek variable hai jisme same type ki multiple values ek saath, ek line mein store hoti hain.
๐ Real Life ExampleEgg tray jaisa socho โ ek hi tray mein 12 eggs (same type ki cheezein) rakhi hoti hain, aur har egg ka apna fixed position (index) hota hai โ pehla egg, dusra egg, etc.
int marks[5] = {90, 85, 76, 60, 45};
printf("First student marks: %d", marks[0]); // index 0 se start
Two-Dimensional Array
Rows aur columns ke form mein data store karta hai.
๐ ExampleCinema hall ki seating โ Row A, Row B, aur har row mein seat number 1,2,3... Ye bilkul 2D array jaisa structure hai.
Address calculate karne ka formula: Base Address + i * size of data type. Length: sizeof(arr) / sizeof(arr[0]).
int arr[5] = {10, 20, 30, 40, 50};
int length = sizeof(arr) / sizeof(arr[0]);
printf("Length = %d", length);
Array Traversing
Array ke saare elements ko ek-ek karke visit karna (loop se).
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
Linear Search
Array mein ek element ko shuru se end tak dhundhna.
๐ Real Life Example
Jaise tum ek bina-sort ki hui phonebook mein kisi naam ko dhundho โ ek-ek entry ko upar se neeche check karte jaate ho, jab tak naam mil na jaye.
int arr[5] = {10, 25, 30, 45, 50}, key = 30;
for (int i = 0; i < 5; i++) {
if (arr[i] == key) {
printf("Found at index %d", i);
break;
}
}
Strings & Predefined Functions
String Kya Hai?
String characters ka ek array hota hai jo \0 (null character) pe khatam hota hai.
๐ Real Life Example
Tumhare name tag pe likhe letters ek string hain โ "R-A-H-U-L" โ jo milke ek naam banate hain.
Function code ka ek chhota block hai jo ek specific task karta hai, aur jise baar-baar call kiya ja sakta hai.
๐ Real Life Example
Ek mixer-grinder jaisa socho โ tum usme ingredients (input/parameters) daalte ho, wo blend karta hai (processing), aur tumhe juice (return value/output) milta hai. Har baar naya mixer banane ki zarurat nahi, same mixer baar-baar use karte ho.
int add(int a, int b); // Declaration/Prototype
int main() {
int result = add(5, 3); // Function Call
printf("Sum = %d", result);
return 0;
}
int add(int a, int b) { // Definition
return a + b;
}
Kyun Zaroori Hai
Function se code reusable banta hai (baar baar likhna nahi padta), program modular (chhote parts mein) hota hai, aur galti dhundhna easy ho jata hai.
Parameter Passing
Call by Value โ function ko variable ki copy milti hai, original change nahi hota. Call by Reference โ function ko variable ka address milta hai, original change ho sakta hai.
๐ Real Life Example
Call by Value = tum kisi ko apne document ki photocopy dete ho โ usmein wo jo marks kare, original document safe rehta hai. Call by Reference = tum unhe original document hi de dete ho โ ab wo usmein direct changes kar sakte hain.
void changeValue(int x) { x = 100; } // copy change hoti hai
void changeValueRef(int *x) { *x = 100; } // original change hota hai
Recursive Functions
Recursion Kya Hai?
Aisa function jo khud ko call karta hai, jab tak ek fixed "base case" na aa jaye.
๐ Real Life ExampleDo mirrors ek dusre ke saamne โ jinme ek image andar image, andar image dikhti chali jaati hai. Ya phir Russian Matryoshka dolls โ ek doll ke andar chhoti doll, uske andar aur chhoti doll โ jab tak sabse chhoti doll (base case) na aa jaye.
int factorial(int n) {
if (n == 0) // base case โ yahi pe rukega
return 1;
return n * factorial(n - 1); // khud ko call kar raha hai
}
// factorial(5) = 5*4*3*2*1 = 120
Kyun Zaroori Hai
Base case na ho to function infinite baar khud ko call karta rahega โ jaise mirror ka reflection kabhi na ruke โ aur program crash (stack overflow) ho jayega.