Formatted and Unformatted I/O in C
You now know six input/output (I/O) functions: printf, scanf, getchar, putchar, fgets and puts. Exams love to ask: "Classify them as formatted and unformatted, with differences."
This short lesson puts them all in one place, with a comparison table you can write directly in your exam.
What You Will Learn
- What console I/O functions are.
- What formatted and unformatted I/O mean.
- Which functions belong to each group.
- The difference table.
- When to use which function.
Formatted and Unformatted I/O in C Explained
Exam definition: Formatted I/O functions (printf, scanf) use format specifiers to read or print data of any type in a chosen format. Unformatted I/O functions (getchar, putchar, gets, fgets, puts) work only with characters and strings and do not use format specifiers.
Classification of console I/O functions
| Input | Output | |
|---|---|---|
| Formatted | scanf() | printf() |
| Unformatted: character | getchar() | putchar() |
| Unformatted: string | fgets() (old: gets()) | puts() |
Console means the keyboard (input) and the screen (output). All these functions are declared in stdio.h.
Memory trick: If it has an f at the end for "format" (printf, scanf), it is formatted. Careful: fgets starts with f for "file", and it is unformatted.
Difference between formatted and unformatted I/O
| Formatted I/O | Unformatted I/O |
|---|---|
Uses format specifiers (%d, %f...) | No format specifiers |
| Works with all data types: int, float, char, string | Works only with characters and strings |
| Can read/print many values in one call | One character or one string per call |
| Output can be formatted (width, precision) | No formatting control |
| Slightly slower (must process the format) | Simpler and slightly faster |
Examples: printf(), scanf() | Examples: getchar(), putchar(), fgets(), puts() |
Syntax Summary of All Console I/O Functions
printf("format", values...); /* formatted output */
scanf("format", &variables...); /* formatted input */
ch = getchar(); /* read one character */
putchar(ch); /* print one character */
fgets(str, sizeof(str), stdin); /* read one line */
puts(str); /* print one line */
Example 1: Formatted and Unformatted I/O in One Program
#include <stdio.h>
#include <string.h>
int main(void)
{
char name[30];
int year;
char section;
printf("Name: ");
fgets(name, sizeof(name), stdin); /* unformatted: a line */
name[strcspn(name, "\n")] = '\0';
printf("Year: ");
scanf("%d", &year); /* formatted: an int */
printf("Section: ");
scanf(" %c", §ion); /* formatted: a char */
puts("--- Student card ---"); /* unformatted: a string */
printf("%s, year %d, section ", name, year); /* formatted */
putchar(section); /* unformatted: a char */
putchar('\n');
return 0;
}
Output
Name: Riya Year: 3 Section: A --- Student card --- Riya, year 3, section A
Code Explanation
fgetsreads the name as a full line (unformatted).scanf("%d", ...)converts the typed text "3" into the number 3 (formatted).scanf(" %c", ...)reads one letter; the space before%cskips the Enter left by the previous input.putsprints a heading line;printfmixes text with values;putcharprints one character.
How to Choose the Right Function
- Need a number (int, float)? →
scanfto read,printfto print. - Need one character? →
getchar/putchar(orscanf(" %c")). - Need a line with spaces? →
fgetsto read,putsorprintfto print. - Need neat columns or decimals? →
printfwith width and precision.
Example 2: Same Output, Formatted vs Unformatted
#include <stdio.h>
int main(void)
{
printf("OK\n"); /* formatted */
puts("OK"); /* unformatted string */
putchar('O'); /* unformatted characters */
putchar('K');
putchar('\n');
return 0;
}
Output
OK OK OK
All three lines look the same. But only printf could also print a number like %d in the same line.
Important Points to Remember
- Formatted: printf, scanf.
- Unformatted: getchar, putchar, gets/fgets, puts.
- Formatted functions use format specifiers and work with all types.
- Unformatted functions work with characters and strings only.
- All are declared in
stdio.h.
Common Mistakes Students Make
| Wrong answer | Correct answer |
|---|---|
| fgets is formatted because it starts with f | fgets is unformatted (f = file) |
| puts can print a number directly | puts prints only strings; use printf for numbers |
| getchar reads a whole word | getchar reads one character |
| scanf is unformatted | scanf is formatted (uses %d etc.) |
Formatted vs Unformatted I/O Practice Questions
- What are console I/O functions?
- Classify printf, getchar, puts, scanf, fgets, putchar.
- Write three differences between formatted and unformatted I/O.
- Which function would you use to read a full address with spaces?
- Which header file declares all these functions?
Show answers
- Functions that read from the keyboard and print to the screen.
- Formatted: printf, scanf. Unformatted: getchar, puts, fgets, putchar.
- Formatted uses format specifiers, works with all data types, and can handle many values per call; unformatted has no specifiers, works only with chars/strings, one item per call.
fgets()stdio.h
Coding Exercise
Read a city name with fgets and a temperature (int) with scanf. Print the city with puts, then the temperature with printf.
Show solution
#include <stdio.h>
#include <string.h>
int main(void)
{
char city[40];
int temp;
printf("City: ");
fgets(city, sizeof(city), stdin);
city[strcspn(city, "\n")] = '\0';
printf("Temperature: ");
scanf("%d", &temp);
puts(city);
printf("Today: %d C\n", temp);
return 0;
}
City: New Delhi Temperature: 31 New Delhi Today: 31 C
Quick Quiz: Formatted and Unformatted I/O (MCQ)
-
Which is a formatted input function?
A)
getchar()B)fgets()C)scanf()D)puts() -
Which is an unformatted output function?
A)
printf()B)putchar()C)scanf()D)fgets() -
Unformatted I/O functions work with:
A) All data types B) Only int C) Only characters and strings D) Only float
-
Which can print an int and a float in one call?
A)
puts()B)putchar()C)printf()D)fgets() -
"Console" in console I/O means:
A) Files B) Keyboard and screen C) Printer D) Internet
Show answers
- C:
scanf() - B:
putchar() - C: only characters and strings.
- C:
printf() - B: keyboard and screen.
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| Formatted input / output | scanf() / printf() |
|---|---|
| Character input / output | getchar() / putchar() |
| String input / output | fgets() (old gets()) / puts() |
| Formatted works with | All data types |
| Unformatted works with | Characters and strings |
| Header | stdio.h |
Frequently asked questions
- What is the main difference between formatted and unformatted I/O?
- Formatted uses format specifiers for any data type; unformatted handles only characters and strings.
- Is fgets formatted or unformatted?
- Unformatted. The f stands for file, not format.
- Which is faster: printf or puts?
- puts is simpler and slightly faster, because it does no format processing.
Remember
- Format-f at the end = formatted: printf, scanf.
- Pairs: getchar/putchar, fgets/puts, scanf/printf.
Next Topic
Next lesson: Width and Precision in printf(). You will learn to print neat columns and control decimal places with printf.