Skip to main content

Formatted vs unformatted I/O

Short answer

Formatted I/O uses format specifiers like %d and %f to control conversion and handle all data types; functions include scanf and printf. Unformatted I/O works with characters and strings as-is, using getchar, putchar, gets, puts, and is simpler. Example: scanf("%d",&age) reads an integer, while ch=getchar() reads a character.

What are the differences between formatted and unformatted input/output functions? Write down the syntax and give suitable examples.

Formatted I/O functions read or display data according to a format given by the programmer (type, width, precision). Unformatted I/O functions read or write a single character or string as it is, without any format specification.

FormattedUnformatted
Uses format specifiers (%d, %f, %c)No format specifiers
Can handle all data typesHandle only char and strings
Output can be controlled by width, precisionNo control over appearance
scanf(), printf(), fscanf(), fprintf()getchar(), putchar(), gets(), puts(), getch()
Slower, more flexibleFaster, simpler

Syntax

  • printf("format string", variable_list);
  • scanf("format string", &variable_list);
  • ch = getchar();, putchar(ch);, puts("string");

Example

#include <stdio.h>
int main(void)
{
    int age;
    char ch;
    printf("Enter age: ");
    scanf("%d", &age);              /* formatted input */
    printf("Age = %d\n", age);      /* formatted output */
    getchar();                      /* consume newline */
    printf("Enter a character: ");
    ch = getchar();                 /* unformatted input */
    printf("You typed: ");
    putchar(ch);                    /* unformatted output */
    putchar('\n');
    puts("Done");                   /* unformatted output of a string */
    return 0;
}

Output (with input 25 and Z):

Enter age: Age = 25
Enter a character: You typed: Z
Done

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 4 questions →