1. Gyanastra
  2. Data Structures
  3. Input and Output
  4. Need for Input and Output in Programs
Free lesson, no account needed 3 min read 8 practice questions after it

Data Structures · Input and Output

Need for Input and Output in Programs

Overview

Every program is written to process something, so it needs input, processing, and output. Understanding how data moves from an input device into memory and from memory to an output device is the foundation for reading values into a program and writing results out of it.

Key concepts

  • Input operation: moving data from an input device (such as the keyboard) into computer memory, from where the program reads the values.
  • Output operation: moving data from computer memory to an output device.
  • Program structure: a program does three things — take the input, process the data, give the output.
  • Interactive program: the user types values at the keyboard and the program reads them from the keyboard.
  • File input: if the data is already stored in files, the program can read the data from the files instead of the keyboard.

How it works

  1. Take the input.
  2. Process the data.
  3. Give the output.

For input, data moves along this path:

  • In an interactive program, the user types values at the keyboard; the program reads them from the keyboard.
  • The values read from the keyboard are passed to memory; the program then reads the values from that memory. This is one way.
  • The other way: if the data is already stored in files, the program reads the data from the files.

For output, data moves from computer memory to the output device.

Revision summary

  • Program = Input → Processing → Output.
  • Input path: input device → memory → program.
  • Output path: memory → output device.
  • Two sources of input: keyboard (interactive programs) and files (data already stored).
  • Every programming language must provide a means to read input, process it, and write output.
  • Reading input from the keyboard is covered first; opening a file and reading input from a file comes at the end of the course.

Previous-year exam insights

  • Standard input (stdin), standard output (stdout) and standard error (stderr) are the three predefined streams every C program gets; stdin is normally the keyboard, stdout and stderr normally the screen, and they can be redirected to files.
  • In C, input/output is done through the stdio.h library: printf/puts/putchar write output, scanf/gets/fgets/getchar read input, and each returns a value (number of items or characters transferred, or EOF on failure) that should be checked.
  • scanf with %d, %f, %c, %s skips leading whitespace for numeric and string conversions but not for %c; %s stops at the first whitespace, so it cannot read a line containing spaces (use fgets for that).
  • Buffered I/O collects data in a memory buffer and transfers it in blocks, which is far more efficient than unbuffered character-by-character I/O; stdout is line-buffered on a terminal and fully buffered when redirected to a file.
  • A stream is an abstract sequence of bytes connecting a program to a device or file; the same read/write functions work on any stream, which is why keyboard input and file input can be handled by the same code.
  • Command-line arguments (argv/argc in C, args in Java) are another input source besides the keyboard and files, supplied when the program is launched.
Pages 1–2 of 2