Input & Output – printf, scanf
Input Output – printf scanf
Output with printf()
In C, printf() is the standard function to display output on the screen. It is defined in <stdio.h>.
Syntax: printf("format string", variables);
#include <stdio.h> int main() { printf("Hello, World!\n"); // Simple string printf("My age is %d\n", 20); // Integer printf("Price: %.2f\n", 99.99f); // Float, 2 decimals printf("Grade: %c\n", 'A'); // Character printf("%d + %d = %d\n", 5, 3, 5+3); // Multiple values return 0; }
Format Specifiers
Format specifiers tell printf/scanf what type of data to expect or print.
| Specifier | Data Type | Example | Output |
|---|---|---|---|
%d | int | printf("%d", 42) | 42 |
%i | int (same as %d) | printf("%i", 42) | 42 |
%f | float | printf("%f", 3.14f) | 3.140000 |
%.2f | float, 2 decimals | printf("%.2f", 3.14f) | 3.14 |
%lf | double | printf("%lf", 3.14) | 3.140000 |
%c | char | printf("%c", 'A') | A |
%s | string | printf("%s", "Hello") | Hello |
%ld | long int | printf("%ld", 100L) | 100 |
%lld | long long int | printf("%lld", 99999LL) | 99999 |
%u | unsigned int | printf("%u", 200) | 200 |
%o | Octal | printf("%o", 8) | 10 |
%x | Hexadecimal | printf("%x", 255) | ff |
%p | Pointer address | printf("%p", ptr) | 0x7fff... |
%% | Literal % | printf("100%%") | 100% |
Width and Precision Formatting
printf("%5d\n", 42); // " 42" — right-aligned, width 5 printf("%-5d|\n", 42); // "42 |" — left-aligned, width 5 printf("%05d\n", 42); // "00042" — zero-padded, width 5 printf("%.4f\n", 3.14159f); // "3.1416" — 4 decimal places printf("%8.2f\n", 3.14f); // " 3.14" — width 8, 2 decimals
Escape Sequences
Special characters that can't be typed directly.
| Escape Seq. | Meaning | Effect |
|---|---|---|
\n | Newline | Moves to next line |
\t | Tab | Inserts horizontal tab (8 spaces) |
\\ | Backslash | Prints a \ |
\" | Double quote | Prints a " |
\' | Single quote | Prints a ' |
\r | Carriage return | Moves to beginning of line |
\0 | Null character | String terminator |
\a | Bell | Audio beep (system) |
\b | Backspace | Moves cursor back one position |
Input with scanf()
scanf() reads input from the user. The & (address-of) operator is required before variable names (except strings).
Syntax: scanf("format", &variable);
#include <stdio.h> int main() { int age; printf("Enter your age: "); scanf("%d", &age); // & is required — gives address of 'age' printf("You are %d years old.\n", age); return 0; }
Forgetting & in scanf causes undefined behavior (program crash or wrong output). Always write scanf("%d", &variable) not scanf("%d", variable). Exception: char arrays/strings don't need &.
Reading Multiple Inputs
#include <stdio.h> int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); // Two integers, space-separated printf("Sum = %d\n", a + b); float price; char grade; scanf("%f %c", &price, &grade); return 0; }
Complete Programs
Program: Simple Calculator
#include <stdio.h> int main() { float a, b; printf("Enter two numbers: "); scanf("%f %f", &a, &b); printf("Sum = %.2f\n", a + b); printf("Difference = %.2f\n", a - b); printf("Product = %.2f\n", a * b); if (b != 0) printf("Division = %.2f\n", a / b); else printf("Division by zero!\n"); return 0; }
getchar() and putchar()
getchar() reads a single character. putchar() prints a single character.
#include <stdio.h> int main() { char ch; printf("Enter a character: "); ch = getchar(); printf("You entered: "); putchar(ch); putchar('\n'); return 0; }
Summary
printf()is used to display output — requires format specifiersscanf()reads user input — requires&before variable names- Format specifiers:
%d(int),%f(float),%lf(double),%c(char),%s(string) - Escape sequences:
\n(newline),\t(tab),\\(backslash) - Width/precision:
%5d,%.2f,%8.3lf - Never forget
&in scanf — causes undefined behavior!
printf() से Output
C में printf() screen पर output display करने का standard function है। यह <stdio.h> में define है।
Syntax: printf("format string", variables);
#include <stdio.h> int main() { printf("Namaste Duniya!\n"); printf("Meri age hai %d\n", 20); printf("Price: %.2f\n", 99.99f); printf("Grade: %c\n", 'A'); return 0; }
Format Specifiers
| Specifier | Data Type | उपयोग |
|---|---|---|
%d | int | Integer print/input करने के लिए |
%f | float | Float number के लिए |
%.2f | float | 2 decimal places के साथ float |
%lf | double | Double precision के लिए |
%c | char | Single character के लिए |
%s | string | String के लिए |
%% | — | % symbol print करने के लिए |
Escape Sequences
| Sequence | अर्थ | Effect |
|---|---|---|
\n | Newline | अगली line पर जाता है |
\t | Tab | Horizontal tab (8 spaces) insert करता है |
\\ | Backslash | \ print करता है |
\" | Double quote | " print करता है |
\0 | Null | String terminator |
scanf() से Input
scanf() user से input पढ़ता है। Variable names से पहले & (address-of operator) लगाना ज़रूरी है।
#include <stdio.h> int main() { int age; printf("Apni age enter karein: "); scanf("%d", &age); // & ज़रूरी है! printf("Aapki age %d hai.\n", age); return 0; }
scanf में & भूलने से program crash हो सकता है या गलत output आ सकता है। हमेशा scanf("%d", &variable) लिखें। Exception: char array/string में & की ज़रूरत नहीं।
Multiple Inputs
int a, b; scanf("%d %d", &a, &b); // दो integers space से separated printf("Sum = %d\n", a + b);
Complete Program
#include <stdio.h> int main() { float a, b; printf("Do numbers enter karein: "); scanf("%f %f", &a, &b); printf("Jod = %.2f\n", a + b); printf("Ghatav = %.2f\n", a - b); printf("Guna = %.2f\n", a * b); if(b != 0) printf("Bhaag = %.2f\n", a / b); return 0; }
सारांश
printf()output display करता है — format specifiers ज़रूरी हैंscanf()user input पढ़ता है — variable से पहले&लगाएं- Format specifiers:
%d(int),%f(float),%c(char),%s(string) - Escape sequences:
\n(newline),\t(tab) - scanf में
&कभी न भूलें!