Lesson 4 — Beginner
Input & Output (printf/scanf)
Program tab tak bekaar hai jab tak user se data le na sake aur result dikha na sake. Is lesson mein hum printf ke format specifiers, scanf ka & wala concept, aur wo common galtiyan dekhenge jo har naye programmer karta hai.
stdio.h — I/O ka base
WHAT
C ka standard input-output stdio.h library se aata hai. printf output dikhata hai, scanf input leta hai. Dono format specifier (%d, %f...) se baat karte hain ki data kis type ka hai.
WHEN
Har program mein — user se value leni ho (marks, naam), ya result dikhana ho (sum, message) — printf/scanf hi use hote hain.
WHERE
Console (terminal) pe default. File ke liye fprintf/fscanf, string ke liye sprintf — same concept, bas target badalta hai.
#include <stdio.h>
int main() {
printf("Line1\nLine2\n"); // nayi line har \n pe
printf("Col1\tCol2"); // tab — 4-8 space jaisa gap
printf("100%% off!"); // % dikhane ke liye %% (double)
return 0;
}
Output:
Line1
Line2
Col1 Col2100% off!
printf aur format specifiers
int i = 25; float f = 3.14159; char c = 'A';
char s[] = "DSWallah"; long big = 123456789L;
printf("%d\n", i); // 25 poora number
printf("%5d|\n", i); // " 25|" width 5 (right align)
printf("%-5d|\n", i); // "25 |" left align
printf("%.2f\n", f); // 3.14 2 decimal places
printf("%c %s\n", c, s); // A DSWallah
printf("%ld\n", big); // 123456789 (long int)
- %d int, %ld long, %f float/double, %c char, %s string, %zu sizeof.
- %5d = kam se kam 5 char width, %-5d = left align, %.2f = 2 decimal tak.
- Type match karo: float ko %d se dikhaya toh garbage output — format specifier aur datatype jaisi ho.
- Printf return value: kitne characters chhape — debugging mein kaam aata hai.
scanf — input lene ka sahi tarika
#include <stdio.h>
int main() {
int age;
char name[50];
printf("Age batao: ");
scanf("%d", &age); // & ZAROORI — address chahiye
printf("Naam batao: ");
scanf("%49s", name); // array ka address pehle se, & nahi
printf("Age=%d, Naam=%s\n", age, name);
return 0;
}
Age batao: 21
Naam batao: Rahul
Output: Age=21, Naam=Rahul
- & (address-of): scanf ko value nahi, value KAHAN store karna hai chahiye — isliye &age. Array ke saath & nahi chahiye kyunki array khud address hai.
- Width lagao: scanf("%49s", buf) — buffer overflow se bachne ka habit banao.
- Return value check: scanf("%d", &x) != 1 ka matlab galat input diya — user ne "abc" type kiya.
- Space/nasline: "%s" word pe rok jaata hai (space chhodta hai). Poori line chahiye toh fgets use karo.
Doosre I/O functions
puts("Hello"); // string print + khud newline
putchar('A'); // ek char print → A
int ch = getchar(); // ek char input (enter tak wait)
putchar(ch);
char buf[100];
fgets(buf, sizeof(buf), stdin); // SAFE line input
puts(buf);
Function Kya karta hai
printf/scanf formatted output/input (most powerful)
puts/gets string output/input (gets = DANGER, avoid)
fgets/fputs file+stdin safe line I/O (use this)
getchar ek char input
putchar ek char output
sprintf string ke andar printf (buffer banao)
Exercise
Question: Float value print karne wala format specifier kya hai?
Question: scanf mein variable ka address lene wala operator kaunsa hai?
Question: Nayi line dikhane wala escape sequence — backslash ke baad ka character kaunsa hai?
Question: gets() function kyun avoid kiya jaata hai? (do words — buffer overflow)
Common mistakes
- scanf mein & chhod dena: scanf("%d", age) galat hai — address chahiye: scanf("%d", &age). Compiler warning ignore kiya toh program crash ho sakta hai.
- Format specifier aur type mismatch: int ko %f se ya float ko %d se dikhana = garbage output. %d int, %ld long, %f float, %c char, %s string — ye map apniyaad rakho.
- Buffer size na rakhna: char name[10] mein %s se 11+ character scan = aas-paas ka data kharab. Hamesha width do (%49s) ya fgets use karo.
Cyber cafe mein form bharte waqt bhi wahi hota hai jo C ke scanf mein hota hai — pehle poochho kya chahiye, phir address (kahan rakhna hai) batao:
#include <stdio.h>
int main() {
int roll; char name[30];
printf("Roll aur Naam: ");
scanf("%d %s", &roll, name);
printf("Roll %d — %s\n", roll, name);
return 0;
}
Roll aur Naam: 101 Priya
Output: Roll 101 — Priya- TCS NQT: format specifier matching + & wale traps — 1-2Q fixed.
- College exam: marks card program (scanf se input, printf se formatted output).
- Interview: gets() kyun unsafe — ye classic follow-up question hai.
- Trap: printf("%d", 3.5) — type mismatch se output garbage, compiler aksar chup rehta hai.
I/O aata hai. Agla lesson — Conditionals — mein seekhenge program ko decision lena kaise sikhate hain (if/else/switch).