C Programming Notes
C Programming Notes
1
5.5 Scope of Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.6 Storage Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.7 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Section 5 Summary (Quick Recap) . . . . . . . . . . . . . . . . . . . . 32
FINAL QUICK REVISION (Poore Syllabus Ka Recap) . . . . . . . . . . . . . 32
SECTION 1: INTRODUCTION
1.1 History of C Language
C language ko Dennis Ritchie ne 1972 mein Bell Labs (AT&T) mein develop kiya
tha. C se pehle B language thi (jo Ken Thompson ne banayi thi), aur B se pehle BCPL
language thi. C ko is liye banaya gaya tha kyunki UNIX operating system ko likhne
ke liye ek powerful, low-level jaisi flexibility wali, lekin high-level jaisi readability wali
language chahiye thi.
Kyun important hai C: - C ek middle-level language hai — matlab ye hardware
ke close bhi kaam kar sakti hai (jaise assembly) aur high-level abstraction bhi deti
hai (jaise loops, functions). - Aaj bhi Operating Systems (Linux kernel), Embedded
Systems, Compilers, Database engines (jaise MySQL) C mein hi likhe jaate hain. - C
ne hi aage chal kar C++, Java, C#, jaise languages ko syntax aur concepts diye.
Common confusion: Log sochte hain C “purani” hone ki wajah se “useless” hai —
lekin aisa nahi hai. C aaj bhi system-level programming ki backbone hai, isliye ise
seekhna zaroori hai.
Har C program ek fixed structure follow karta hai. Ye structure samajhna bahut za-
roori hai kyunki agar aap iska order galat kar doge to compiler error dega.
1. Documentation / Comments (optional - program kya karta hai, yeh batane ke liye)
2. Preprocessor Directives (#include, #define)
3. Global Declarations (global variables, function prototypes)
4. main() function (program yahin se start hota hai)
{
Local variable declarations
Statements / Instructions
return statement
}
5. User-defined functions (agar koi hai to)
2
Example:
/* Yeh program 2 numbers ka sum print karta hai */ // 1. Documentation
Output:
Sum = 15
Explanation line by line: - #include <stdio.h> compiler ko batata hai ki printf
jaisi standard functions ki definitions is header file mein hain, unhe program mein
“include” (copy-paste jaisa) kar do. - int main() woh function hai jahan se program
execution start hota hai. Har C program mein exactly ek main() hona zaroori hai. -
Curly braces { } function ki body (block) ko define karte hain. - return 0; operating
system ko batata hai ki program successfully complete hua (0 = no error).
Common Mistake: Beginners main() ke andar semicolon (;) laga dete hain — jaise
int main(); — yeh galat hai, isse function ka definition alag ho jaata hai aur “unde-
fined reference” error aata hai.
3
#include <stdio.h>
int main() {
greet(); // is function ko call kiya
greet(); // dobara call kiya - reusability
return 0;
}
Output:
Hello Student!
Hello Student!
(a) Character Set C compiler jin characters ko samajhta hai unka set “character
set” kehlata hai: - Alphabets: A-Z, a-z - Digits: 0-9 - Special symbols: + - * / =
( ) { } [ ] , ; : ' " # & ^ ! ~ | \ < > . ? _ % @ - White spaces: space, tab,
newline
(b) C Tokens Program ki sabse chhoti individual unit ko token kehte hain. C com-
piler program ko tokens mein tod kar hi samajhta hai. Tokens 6 types ke hote hain:
Example: int sum = 5 + 3; Yahan tokens hain: int (keyword), sum (identifier), =
(operator), 5 (constant), + (operator), 3 (constant), ; (special symbol).
(c) Keywords Keywords woh reserved words hain jinka pehle se hi ek fixed mean-
ing C compiler mein defined hai. Inhe variable ya function ka naam nahi bana sakte.
Total 32 keywords C mein hote hain, jaise: int, float, char, double, if, else,
for, while, do, switch, case, break, continue, return, void, struct, union,
typedef, static, extern, const, sizeof, goto, enum etc.
4
Common Mistake: int int = 5; — yeh galat hai kyunki int ek keyword hai, ise
variable name nahi bana sakte.
(d) Identifiers Identifiers woh naam hain jo hum khud apne variables, functions,
arrays, etc. ko dete hain.
Rules for naming identifiers: 1. Sirf letters (A-Z, a-z), digits (0-9), aur underscore
(_) use kar sakte hain. 2. Pehla character letter ya underscore hona chahiye, digit
nahi ho sakta. 3. Keywords ko identifier nahi bana sakte. 4. C case-sensitive hai —
matlab Sum aur sum do alag identifiers hain. 5. Koi space ya special symbol (jaise @, #)
allowed nahi.
int roll_no; // valid
int _marks; // valid
int 2ndYear; // INVALID - digit se start nahi ho sakta
int total-marks; // INVALID - hyphen allowed nahi
(e) Variables Variable ek naam diya gaya memory location hai jiski value pro-
gram run hone ke dauraan change ho sakti hai.
int age; // declaration - compiler ko batate hain ki 'age' naam ka int type var
age = 20; // assignment - value store ki
int marks = 90; // declaration + initialization ek saath
Concept samjho: Jab hum int age; likhte hain, to compiler memory mein kahin ek
jagah (address) reserve kar deta hai jiska naam “age” hota hai. Jab hum age = 20
karte hain to us memory location mein value 20 store ho jaati hai.
int main() {
printf("%f", PI);
// PI = 3.5; // ERROR - constant ki value change nahi kar sakte
}
(g) Data Types Data type batata hai ki variable kis tarah ka data store karega aur
kitni memory use karega.
5
Data Type Size (typical) Format Specifier Example
double 8 bytes %lf double c = 5.55555;
char 1 byte %c char d = 'A';
#include <stdio.h>
int main() {
int a = 10;
float b = 5.5;
char c = 'A';
double d = 99.999;
Output:
Int: 10
Float: 5.500000
Char: A
Double: 99.999000
Common Mistake: float ke liye %lf aur printf mein use karna technically chalta
hai (compiler warning de sakta hai), lekin scanf mein float ke liye hamesha %f hi use
karo aur double ke liye %lf — yahan galti hone par garbage value aati hai.
(h) Comments Comments woh lines hain jo compiler ignore kar deta hai — ye sirf
programmer ke samajhne ke liye hote hain.
// Yeh single-line comment hai
/* Yeh
multi-line
comment hai */
6
• Keywords fixed hote hain (32), identifiers hum khud banate hain (kuch rules ke
sath).
• Variables value store karte hain jo change ho sakti hai; constants change nahi
hoti.
• Data types batate hain memory size aur value ka type (int, float, char, double).
SECTION 2: OPERATORS
2.1 Types of Operators
Operator ek symbol hota hai jo compiler ko batata hai ki kisi variable/constant par
konsa mathematical ya logical operation perform karna hai.
#include <stdio.h>
int main() {
int a = 10, b = 3;
Output:
Addition: 13
Is a>b: 1
AND: 1
Modulus: 1
a after += : 15
Modulus (%) ka concept: Yeh sirf integers ke sath kaam karta hai aur division ke
baad remainder deta hai. Jaise 10 % 3 = 1 (kyunki 10, 3 se 3 baar divide hota hai
aur 1 bacha reh jaata hai). Yeh operator odd/even check karne mein bahut use hota
hai.
7
Common Mistake: Beginners = (assignment) aur == (comparison) mein confuse ho
jaate hain.
if (a = 5) // GALAT (logical mistake) - yeh a mein 5 assign kar dega, aur condition h
if (a == 5) // SAHI - yeh check karega ki a ki value 5 hai ya nahi
Common precedence order (high to low): () > * / % > + - > relational > && ||
> =
Example jahan associativity matter karti hai:
int a = 10 - 5 - 2; // subtraction left-to-right associative hai
// (10-5)-2 = 3, na ki 10-(5-2)=7
printf("%d", a); // Output: 3
Common Mistake: Bina brackets use kiye complex expressions likhna, jaise a + b
* c / d - e. Agar confusion ho to hamesha () use karo taaki apni intended order
clearly mile — code readable bhi hota hai.
8
c = temp * 2;
} // compound statement end
C mein input lena aur output dikhana stdio.h header file ke functions se hota hai.
int age;
printf("Enter age: ");
scanf("%d", &age); // '&' address-of operator hai - variable ka memory address deta
printf("Your age is %d", age);
scanf() - Input lene ke liye & kyun zaroori hai scanf mein? scanf ko pata hona
chahiye ki value kahan store karni hai - isliye variable ka address (&age) diya jaata
hai, sirf value (age) nahi. Yeh ek sabse common beginner mistake hai - & bhool jaana.
char ch;
ch = getch(); // conio.h header chahiye (Turbo C / Windows compilers mein)
getch() - Ek character read karta hai, screen par show NAHI karta (echo nahi
hota), aur Enter dabane ki zaroorat nahi
char ch;
ch = getchar(); // stdio.h se aata hai, standard/portable hai
getchar() - Ek character read karta hai standard input se, screen par show
hota hai, Enter dabana padta hai
putchar('A'); // Output: A
9
printf("You entered: ");
putchar(ch); // output kar raha hai
return 0;
}
Header file ek file hoti hai jisme pehle se likhe hue functions ki declarations hoti hain
(jaise printf, scanf ki declarations stdio.h mein hain). Header file include karne se
hum bina khud function likhe unhe use kar sakte hain.
Preprocessor directives woh instructions hain jo actual compilation shuru hone se
pehle process hoti hain. Ye # symbol se start hoti hain.
#define Ek macro (symbolic constant) define karta hai - compile hone se pehle hi
is naam ki jagah value put ho jaati hai.
#include <stdio.h>
#define PI 3.14159 // PI naam ka macro, value 3.14159
#define SQUARE(x) ((x)*(x)) // function-like macro
int main() {
float area, radius = 5;
area = PI * radius * radius;
printf("Area = %f\n", area);
printf("Square of 4 = %d\n", SQUARE(4));
return 0;
}
Output:
Area = 78.539750
Square of 4 = 16
10
Concept samjho: #define mein jo bhi likha hota hai, compilation se pehle hi pre-
processor us naam ko literal text se replace kar deta hai - jaise “Find & Replace”.
Isliye PI ki jagah 3.14159 seedha code mein chala jaata hai compile hone se pehle hi.
Common Mistake: Macro mein brackets na lagana. #define SQUARE(x) x*x likhoge
to SQUARE(2+3) ka result 2+3*2+3 = 11 aayega (galat), jabki ((x)*(x)) likhne se
((2+3)*(2+3)) = 25 sahi aayega. Isliye macros mein hamesha extra brackets lagao.
Output: Pass
(b) if-else statement Condition true ho to if block, false ho to else block execute
hota hai.
11
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 0;
}
Output: Odd
(c) Nested if-else if ya else ke andar dubara if-else use karna. Jab multiple
conditions check karni ho.
#include <stdio.h>
int main() {
int marks = 75;
if (marks >= 90) {
printf("Grade A\n");
} else {
if (marks >= 75) {
printf("Grade B\n");
} else {
printf("Grade C\n");
}
}
return 0;
}
Output: Grade B
Concept samjho: Nested if-else tab use karte hain jab conditions hierarchi-
cal/dependent hoti hain — jaise pehle check karo marks 90+ hain ya nahi, agar
nahi to phir check karo 75+ hain ya nahi. Iski jagah else if ladder bhi use kar sakte
hain jo padhne mein aasan hota hai:
if (marks >= 90) printf("Grade A\n");
else if (marks >= 75) printf("Grade B\n");
else printf("Grade C\n");
(d) switch statement Jab ek hi variable ki multiple specific values check karni ho,
tab switch use karte hain (bahut saari if-else if ki jagah).
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
12
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Invalid day\n");
}
return 0;
}
Output: Wednesday
Common Mistake: break; bhool jaana. Agar break nahi lagate to control agle case
mein bhi chala jaata hai (isse “fall-through” kehte hain), jo galat output de sakta
hai:
switch(2) {
case 1: printf("One\n");
case 2: printf("Two\n"); // yahan se print hoga
case 3: printf("Three\n"); // break na hone ki wajah se yeh bhi print ho jaayega!
}
// Output: Two \n Three (galat agar sirf "Two" chahiye tha)
Loop ek block of code ko baar baar execute karne ke liye use hota hai jab tak ek
condition true rehti hai.
(a) while loop Condition pehle check hoti hai, phir body execute hoti hai (entry-
controlled loop).
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++; // increment - warna infinite loop ho jaayega
}
return 0;
}
Output: 1 2 3 4 5
(b) do-while loop Body pehle ek baar execute hoti hai, uske baad condition check
hoti hai (exit-controlled loop). Isliye body kam se kam ek baar zaroor chalti hai,
chahe condition false ho.
#include <stdio.h>
int main() {
13
int i = 10;
do {
printf("%d ", i);
i++;
} while (i <= 5); // condition false hai, phir bhi 1 baar chala
return 0;
}
Output: 10 (condition false hone ke bawajood ek baar chala kyunki do-while pehle
execute karta hai, phir check karta hai)
(c) for loop Jab humein pata ho ki loop kitni baar chalana hai, tab for loop best
hai — initialization, condition, aur increment/decrement ek hi line mein hoti hai.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) { // (initialization; condition; increment)
printf("%d ", i);
}
return 0;
}
Output: 1 2 3 4 5
(d) Nested for loop For loop ke andar for loop — patterns print karne, matrix op-
erations mein bahut use hota hai.
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) { // outer loop - rows
for (int j = 1; j <= i; j++) { // inner loop - columns
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
*
* *
* * *
Concept samjho: Outer loop ek baar chalta hai to inner loop poora chal kar khatam
hota hai, tab jaake outer loop agli value par jaata hai. Yeh samajhna pattern printing
ke liye bahut zaroori hai.
Common Mistake: for loop mein semicolon galat jagah laga dena:
14
for (int i=0; i<5; i++); // GALAT - yahan semicolon se loop khali body ke sath khatam
{
printf("%d", i); // yeh loop se bahar hai, sirf ek baar chalega
}
continue Current iteration ko skip kar deta hai aur agli iteration par chala jaata hai
(loop khatam nahi hota).
for (int i = 1; i <= 5; i++) {
if (i == 3) continue; // 3 ko skip kar dega
printf("%d ", i);
}
// Output: 1 2 4 5
goto Program control ko kisi specific labeled line par le jaata hai. Modern program-
ming mein iska use avoid kiya jaata hai kyunki yeh code ko samajhna mushkil bana
deta hai (“spaghetti code”).
#include <stdio.h>
int main() {
int i = 1;
start: // label
if (i <= 5) {
printf("%d ", i);
i++;
goto start; // control 'start' label par wapas jaata hai
}
return 0;
}
Output: 1 2 3 4 5
exit() Program ko turant band kar deta hai, chahe woh kahin bhi ho (loop ke andar,
function ke andar). Isko use karne ke liye stdlib.h include karna padta hai.
15
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Before exit\n");
exit(0); // program yahin khatam ho jaayega
printf("After exit\n"); // yeh line kabhi execute nahi hogi
return 0;
}
• if, if-else, nested if-else: condition ke basis par decision lene ke liye.
• switch: ek variable ki multiple fixed values check karne ke liye; break na
bhoolna.
• while: entry-controlled loop (pehle check, phir execute).
• do-while: exit-controlled loop (pehle execute, phir check) - kam se kam 1 baar
chalta hai.
• for: jab iterations ki count pehle se pata ho.
• Nested loops: patterns aur matrix ke liye - outer loop ek baar, inner loop poora
chalta hai.
• break: loop se bahar nikalna; continue: current iteration skip karna; goto: label
par jump; exit(): poora program terminate.
16
return 0;
}
#include <stdio.h>
int main() {
int a, b, product;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
product = a * b;
printf("Product = %d", product);
return 0;
}
Logic: Number ko 2 se modulus (%) karo. Agar remainder 0 hai to even, warna odd.
17
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("Maximum = %d", a);
else
printf("Maximum = %d", b);
return 0;
}
Logic: Loop chalao 1 se N tak, har number ko sum variable mein add karte jao.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum = sum + i; // har baar current i ko sum mein add kar rahe hain
}
printf("Sum = %d", sum);
18
return 0;
}
Logic: Yahan N numbers khud user input karega (fixed range nahi, jo bhi user de).
#include <stdio.h>
int main() {
int n, i, num, sum = 0;
printf("How many numbers? ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
sum = sum + num;
}
printf("Sum = %d", sum);
return 0;
}
Logic: / operator do integers ke beech use hone par sirf quotient deta hai (decimal
part cut ho jaata hai). Remainder ke liye % use karte hain.
#include <stdio.h>
int main() {
int a = 17, b = 5;
printf("Quotient = %d\n", a / b); // integer division
printf("Remainder = %d\n", a % b);
return 0;
}
Output:
Quotient = 3
Remainder = 2
Common Mistake: int / int hamesha int result deta hai. Agar decimal chahiye to
kam se kam ek operand ko float banana padta hai: (float)a / b.
19
4.9 Digit Reversing
Logic: Number ka last digit %10 se nikalo, use reverse number mein jodo, phir original
number ko /10 karke last digit hata do — jab tak number 0 na ho jaaye.
#include <stdio.h>
int main() {
int num, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10; // last digit nikala
rev = rev * 10 + digit; // reverse number mein jodo
num = num / 10; // last digit hata diya
}
printf("Reversed number = %d", rev);
return 0;
}
#include <stdio.h>
int main() {
int n, i;
printf("Enter number: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", n, i, n * i);
}
return 0;
}
Input: 5 → Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
20
4.11 Factorial of a Number
21
return f;
}
Concept samjho: Sine series ek infinite series hai jo trigonometric sin(x) ki value
approximate karti hai bina calculator/library function use kiye. Har term mein power
aur factorial dono badhte jaate hain, aur sign alternate (+, -, +, -) hota hai — isliye
pow(-1, i) use kiya jaata hai sign flip karne ke liye.
Note: Compile karte waqt -lm flag lagana padta hai math functions ke liye: gcc
file.c -o file -lm
int main() {
float x, sum = 0, term;
int n, i;
printf("Enter x (radians) and number of terms: ");
scanf("%f %d", &x, &n);
Logic: Har row mein binomial coefficients hote hain. Simplest approach: nested loop
se nCr formula use karna, ya pichli value se next value calculate karna.
#include <stdio.h>
int main() {
int rows, i, j, num;
22
printf("Enter number of rows: ");
scanf("%d", &rows);
Input: 5 → Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Concept samjho: Har number apne upar wali row ke do numbers ka sum hota hai (jo
humne yahan formula num*(i-j)/(j+1) se directly calculate kiya, taaki nested loop
se dubara calculation na karni pade).
Logic: Number sirf 1 aur khud se divide hota ho (2 se lekar sqrt(n) tak koi divisor na
ho) to woh prime hai.
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) {
isPrime = 0; // 1 aur usse chhote numbers prime nahi hote
} else {
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0; // agar koi divisor mil gaya to prime nahi hai
break;
}
}
}
23
if (isPrime)
printf("%d is Prime", num);
else
printf("%d is Not Prime", num);
return 0;
}
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factors of %d: ", num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Logic: Ek number “perfect” hota hai agar uske saare factors (khud ko chhod kar) ka
sum khud number ke barabar ho. Jaise 6 = 1+2+3.
#include <stdio.h>
int main() {
int num, i, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
24
}
}
if (sum == num)
printf("%d is a Perfect Number", num);
else
printf("%d is Not a Perfect Number", num);
return 0;
}
Logic (Euclidean Algorithm): GCD(a,b) = GCD(b, a%b), jab tak b=0 na ho jaaye —
tab a hi answer hoga.
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
while (b != 0) {
temp = b;
b = a % b;
a = temp;
}
printf("GCD = %d", a);
return 0;
}
25
temp = a; // a ki value temp mein save
a = b; // b ki value a mein
b = temp; // temp (original a) ki value b mein
Output:
Before swap: a=5, b=10
After swap: a=10, b=5
Method 2: Without temporary variable (using arithmetic)
a = a + b;
b = a - b; // yeh original a ke barabar ho jaata hai
a = a - b; // yeh original b ke barabar ho jaata hai
Common Mistake: Method 2 mein overflow ho sakta hai agar a+b variable ki max-
imum limit se bada ho jaaye — isliye temp variable wala method safer aur zyada
preferred hai.
SECTION 5: FUNCTIONS
Function ek self-contained block of code hai jo ek specific task perform karta hai. Ye
code ko organize, reusable aur readable banata hai.
1. Library functions (built-in) - pehle se C library mein defined hote hain, jaise
printf(), sqrt(), strlen().
2. User-defined functions - programmer khud banata hai apni zaroorat ke hisaab
se, jaise add(), factorial().
26
5.2 Function Declaration, Definition aur Call
Full example:
#include <stdio.h>
int main() {
int result = add(5, 3); // 3. Call
printf("Sum = %d", result);
return 0;
}
Output: Sum = 8
Concept samjho: Declaration compiler ko sirf ek “promise” deta hai ki function aage
kahin defined milega, taaki compiler main() mein call ko sahi se samajh sake, chahe
actual definition function ke baad mein likhi ho. Agar declaration na ho aur definition
main() ke baad ho, to compiler error de sakta hai (“implicit declaration”).
Type Example
No arguments, no return value void greet()
No arguments, return value int getNumber()
Arguments, no return value void display(int x)
Arguments, return value int add(int a, int b)
27
Type Example
#include <stdio.h>
int main() {
greet();
int n = getNumber();
display(n);
printf("Sum = %d", add(3, 4));
return 0;
}
Output:
Hello!
Value = 100
Sum = 7
Call by Value Function ko variable ki copy milti hai. Function ke andar changes
sirf uss copy par hote hain, original variable change nahi hota.
#include <stdio.h>
void modify(int x) {
x = x + 10; // sirf local copy change ho rahi hai
printf("Inside function: x = %d\n", x);
}
28
int main() {
int a = 5;
modify(a);
printf("Outside function: a = %d\n", a); // original 'a' unchanged
return 0;
}
Output:
Inside function: x = 15
Outside function: a = 5
void modify(int *x) { // pointer parameter - address receive kar raha hai
*x = *x + 10; // *x se original variable ki value access/change ho ra
printf("Inside function: x = %d\n", *x);
}
int main() {
int a = 5;
modify(&a); // 'a' ka address pass kar rahe hain
printf("Outside function: a = %d\n", a); // original 'a' changed!
return 0;
}
Output:
Inside function: x = 15
Outside function: a = 15
Concept samjho: Call by value mein function apna alag memory space use karta hai
(copy), isliye original safe rehta hai. Call by reference mein function seedha original
variable ke memory address par kaam karta hai, isliye original bhi change ho jaata
hai. Yeh concept swapping do numbers using functions jaisi problems mein bahut
zaroori hota hai — swap sirf call by reference se hi possible hai, call by value se nahi.
Common Mistake: Swap function call by value se likhna aur expect karna ki original
values swap ho jaayengi — yeh kabhi nahi hoga, kyunki function ke paas sirf copies
hoti hain.
Scope batata hai ki variable program ke kis part mein accessible/visible hai.
29
1. Local variables - kisi function/block ke andar declare hote hain, sirf usi function
ke andar accessible hote hain.
2. Global variables - function ke bahar declare hote hain, poore program mein
kahin bhi accessible hote hain.
#include <stdio.h>
void display() {
int localVar = 5; // local variable - sirf is function ke andar accessible
printf("Local: %d, Global: %d\n", localVar, globalVar);
}
int main() {
printf("Global from main: %d\n", globalVar); // yeh access ho sakta hai
display();
// printf("%d", localVar); // ERROR - localVar yahan accessible nahi hai
return 0;
}
Output:
Global from main: 100
Local: 5, Global: 100
Common Mistake: Global variables ka zaroorat se zyada use karna — yeh code ko
debug karna mushkil bana deta hai kyunki koi bhi function unko kahin se bhi change
kar sakta hai. Best practice hai local variables aur parameters ka use karna, jab tak
global zaroori na ho.
Storage class batati hai ki variable ka scope, lifetime, aur default value kya hoga.
30
#include <stdio.h>
void counter() {
static int count = 0; // static - value function calls ke beech bhi retain hoti h
count++;
printf("Count = %d\n", count);
}
int main() {
counter(); // Count = 1
counter(); // Count = 2
counter(); // Count = 3
return 0;
}
Output:
Count = 1
Count = 2
Count = 3
Concept samjho: Normal (auto) local variable har function call mein naya banta
hai (reset ho jaata hai), lekin static variable sirf ek baar initialize hota hai aur uski
value function calls ke beech bhi yaad rehti hai. Yahan agar static nahi likhte, to
har call mein count 0 se start hokar 1 hi print karta, kabhi 2 ya 3 nahi.
extern use hota hai jab variable kisi doosri file mein defined ho aur usse current file
mein use karna ho (multi-file programs mein).
5.7 Recursion
Recursion ek technique hai jisme function khud ko call karta hai. Har recursive
function mein 2 cheezein zaroori hain: 1. Base case - woh condition jahan recursion
rukta hai (warna infinite recursion ho jaayega aur stack overflow error aayega). 2.
Recursive case - jahan function khud ko smaller problem ke sath call karta hai.
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) // BASE CASE - yahan recursion rukta hai
return 1;
else
return n * factorial(n - 1); // RECURSIVE CASE - khud ko chhote problem ke sa
}
int main() {
int result = factorial(5);
printf("Factorial = %d", result);
31
return 0;
}
32
Topic Key Point
History C - Dennis Ritchie, 1972, Bell Labs,
UNIX ke liye
Structure Preprocessor → Global declarations →
main() → user functions
Tokens Keywords, Identifiers, Constants,
Strings, Operators, Symbols
Data Types int, float, char, double (sath mein format
specifiers %d %f %c %lf)
Operators Arithmetic, Relational, Logical,
Assignment, Increment/Decrement,
Bitwise, Ternary
I/O printf/scanf (stdio.h), getch (conio.h,
non-standard), getchar/putchar
(portable)
Preprocessor #include (header add), #define
(macro/constant)
Control Structures if/if-else/nested-if, switch (break zaroori),
while/do-while/for,
break/continue/goto/exit
Arithmetic Problems Sabme ek common pattern: input →
loop/condition → process → output
Functions Declaration+Definition+Call; Call by
value (copy) vs Call by reference
(address); Scope (local/global); Storage
classes (auto/static/extern/register);
Recursion (base case + recursive case)
All the best for your exam! Practice karte raho, especially code likh kar khud
dry-run karo — usse concepts pakke ho jaayenge.
33