Formatted & Unformatted Input/Output (I/O) Operations
Unit 1: Algorithms, Flowcharts & C Program Architecture • Problem Solving Using C
👨🏫 Professor's Mental Model: The Stencil Printing Press vs Raw Unsorted Mail Stream
Formatted I/O (printf, scanf) ek customized printing stencil jaisa hai: aap specify karte ho ki number kitne decimal places me dikhega (%0.2f), ya text kitne blocks me right-align hoga (%10s). Jabki Unformatted I/O (getchar, putchar, gets, puts) ek simple conveyor belt jaisa hai jo bina kisi formatting ya translation ke raw characters ko ek-ek karke seedha andar ya bahar bhejta hai!
Academic Lecture Notes & Solved Study Pages
Unit 1 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. The 3 Standard I/O Streams in C
Every executing C program is automatically connected to 3 standard text streams via `<stdio.h>`:
2. Formatted Output with printf() & Field Width Modifiers
General Syntax: `printf("format string", arg1, arg2, ...);` Format Specifier Structure: `%[flags][width][.precision][length]specifier`
3. Formatted Input with scanf() & The Address-of (&) Operator
General Syntax: `scanf("format string", &var1, &var2, ...);`
Why is the Address-of (&) operator mandatory in scanf()? In C, function arguments are passed by value. To allow scanf() to write incoming keyboard data directly into your variable's memory cell in RAM, you MUST supply the variable's physical memory address (`&variable`).
Exception for Strings: Array names (like `char name[50]`) decay into a pointer representing their base address (`&name[0]`), so writing `&` is unnecessary: `scanf("%s", name);`.
Limitation: `scanf("%s", str)` terminates input at the first encountered whitespace (space, tab, newline). It cannot read multi-word strings like "New Delhi"!
4. Unformatted Character & String I/O Functions
| Parameter | Formatted I/O (printf / scanf) | Unformatted I/O (getchar / fgets / puts) |
|---|---|---|
| Data Types Supported | All primitive data types (int, float, char, double, string) | Only individual characters and character strings |
| Control Over Layout | High - controls field width, precision, padding, decimal places | None - outputs raw characters without formatting |
| Speed & Overhead | Slower due to format string parsing and binary conversion | Faster - performs direct byte transfers |
| Header File | Mandatory <stdio.h> | <stdio.h> (and <conio.h> for getch) |
| Space Handling | scanf("%s") stops at first space; scanf("%[^ ]") reads line | fgets() reads spaces until newline or buffer size limit |
Interactive Tested Code Example
#include <stdio.h>
int main(void) {
int item_id = 101;
char item_name[] = "Scientific Calculator";
float price = 850.50f;
int quantity = 2;
float total = price * quantity;
printf("================================================\n");
printf(" TAX INVOICE RECEIPT \n");
printf("================================================\n");
printf("%-10s %-25s %5s %10s\n", "ITEM ID", "DESCRIPTION", "QTY", "TOTAL (RS)");
printf("------------------------------------------------\n");
printf("%-10d %-25s %5d %10.2f\n", item_id, item_name, quantity, total);
printf("================================================\n");
printf("Leading Zero Formatting Demo: Invoice #%06d\n", item_id);
return 0;
}🎯 University Exam Scoring Blueprint
- In exam questions, always point out why gets() is deprecated and demonstrate the safe fgets(buffer, sizeof(buffer), stdin) alternative.
- Explain the format string structure: %[flags][width][.precision]type.
- Remember to state that fflush(stdin) is used to clear residual newline characters from the input buffer before reading characters.