Skip to content
StudyFame

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

InputOutput
Formattedscanf()printf()
Unformatted: charactergetchar()putchar()
Unformatted: stringfgets() (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/OUnformatted I/O
Uses format specifiers (%d, %f...)No format specifiers
Works with all data types: int, float, char, stringWorks only with characters and strings
Can read/print many values in one callOne 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", &section);                 /* 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

  1. fgets reads the name as a full line (unformatted).
  2. scanf("%d", ...) converts the typed text "3" into the number 3 (formatted).
  3. scanf(" %c", ...) reads one letter; the space before %c skips the Enter left by the previous input.
  4. puts prints a heading line; printf mixes text with values; putchar prints one character.

How to Choose the Right Function

  • Need a number (int, float)? → scanf to read, printf to print.
  • Need one character? → getchar / putchar (or scanf(" %c")).
  • Need a line with spaces? → fgets to read, puts or printf to print.
  • Need neat columns or decimals? → printf with 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 answerCorrect answer
fgets is formatted because it starts with ffgets is unformatted (f = file)
puts can print a number directlyputs prints only strings; use printf for numbers
getchar reads a whole wordgetchar reads one character
scanf is unformattedscanf is formatted (uses %d etc.)

Formatted vs Unformatted I/O Practice Questions

  1. What are console I/O functions?
  2. Classify printf, getchar, puts, scanf, fgets, putchar.
  3. Write three differences between formatted and unformatted I/O.
  4. Which function would you use to read a full address with spaces?
  5. Which header file declares all these functions?
Show answers
  1. Functions that read from the keyboard and print to the screen.
  2. Formatted: printf, scanf. Unformatted: getchar, puts, fgets, putchar.
  3. 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.
  4. fgets()
  5. 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)

  1. Which is a formatted input function?

    A) getchar()   B) fgets()   C) scanf()   D) puts()

  2. Which is an unformatted output function?

    A) printf()   B) putchar()   C) scanf()   D) fgets()

  3. Unformatted I/O functions work with:

    A) All data types   B) Only int   C) Only characters and strings   D) Only float

  4. Which can print an int and a float in one call?

    A) puts()   B) putchar()   C) printf()   D) fgets()

  5. "Console" in console I/O means:

    A) Files   B) Keyboard and screen   C) Printer   D) Internet

Show answers
  1. C: scanf()
  2. B: putchar()
  3. C: only characters and strings.
  4. C: printf()
  5. 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 / outputscanf() / printf()
Character input / outputgetchar() / putchar()
String input / outputfgets() (old gets()) / puts()
Formatted works withAll data types
Unformatted works withCharacters and strings
Headerstdio.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.