In C programming, Input/Output (I/O) operations allow us to communicate with the user. Input refers to getting data from the user, while output refers to displaying data to the user. This unit focuses on both formatted and unformatted I/O functions.
1. Input/Output Operations in C
- Input: Data entered by the user (keyboard, file, etc.).
- Output: Data displayed to the user (screen, printer, file).
- Handled through standard library functions defined in <stdio.h>.
2. Formatted I/O Functions
Formatted I/O functions allow input/output in a specific format.
(a) printf() – Formatted Output
- Used to display output on the screen.
Syntax:
printf(“format string”, variable_list);
Example:
int age = 20;
float pi = 3.1416;
printf(“Age = %d, PI = %.2f”, age, pi);
Output:
Age = 20, PI = 3.14
Common Format Specifiers:
- %d → integer
- %f → float
- %c → character
- %s → string
- %.2f → float with 2 decimal places
(b) scanf() – Formatted Input
- Used to take input from the user.
Syntax:
scanf(“format string”, &variable_list);
Example:
int roll;
scanf(“%d”, &roll);
printf(“Roll Number = %d”, roll);
⚠️ Important: Use & (address-of operator) for variables.
3. Unformatted I/O Functions
These functions are used for single character or string input/output, without format control.
(a) getchar() – Input Single Character
- Reads one character from the keyboard.
char c;
c = getchar();
printf(“Character entered: %c”, c);
(b) putchar() – Output Single Character
- Displays one character on screen.
char c = ‘A’;
putchar(c); // Output: A
(c) getch() – Input Character (No Echo)
- Reads a single character without displaying it on screen (commonly used for passwords).
char ch;
ch = getch(); // User presses ‘X’
printf(“%c”, ch); // Output: X
(d) putch() – Output Character
- Prints a single character (similar to putchar()).
putch(‘Z’); // Output: Z
(e) gets() – Input String (deprecated, but still asked in exams)
- Reads a line of text (string) from the user.
char name[30];
gets(name);
printf(“Hello %s”, name);
⚠️ Problem: gets() is unsafe because it allows buffer overflow. Use fgets() in modern C.
(f) puts() – Output String
- Displays a string followed by a newline.
char city[] = “Kathmandu”;
puts(city); // Output: Kathmandu
4. Difference Between Formatted & Unformatted I/O
| Aspect | Formatted I/O (printf, scanf) | Unformatted I/O (getchar, putchar, gets, puts) |
| Control | Provides formatting (e.g., decimals, alignment). | No control, direct input/output. |
| Data Types | Supports multiple types (int, float, char, string). | Usually deals with characters and strings. |
| Safety | Safer (with format specifiers). | Risk of overflow (gets). |
| Example | scanf(“%d”, &a); | a = getchar(); |
5. Example Program – Input & Output
#include <stdio.h>
int main() {
int age;
char name[20];
printf(“Enter your name: “);
gets(name); // Taking string input
printf(“Enter your age: “);
scanf(“%d”, &age);
printf(“\nHello %s, you are %d years old.”, name, age);
return 0;
}
Output:
Enter your name: Sita
Enter your age: 21
Hello Sita, you are 21 years old.
Key Takeaways
- Formatted I/O → printf(), scanf() for structured input/output.
- Unformatted I/O → getchar(), putchar(), gets(), puts(), getch(), putch().
- Always use & with variables in scanf().
- gets() is unsafe → use fgets() in modern C.
- I/O functions are essential for interactive C programs.