In C programming, an input stream is a fundamental mechanism for reading data from various sources such as keyboards, files, or network connections. It represents a sequence of bytes that can be processed sequentially.
Input streams in C can be categorized into different types:
Stream Type |
Description |
Common Use Cases |
Standard Input (stdin) |
Default input from keyboard |
User interaction, console input |
File Input |
Reading from files |
File processing, data loading |
String Input |
Reading from memory strings |
String parsing, buffer manipulation |
Stream Characteristics
graph TD
A[Input Stream] --> B[Sequential Access]
A --> C[Buffered Reading]
A --> D[Character or Block Reading]
Key Properties
- Sequential data access
- Buffered reading mechanism
- Support for different reading methods
C provides several functions for stream input:
getchar()
: Reads a single character
scanf()
: Reads formatted input
fgets()
: Reads a line of text
fscanf()
: Reads formatted input from a specific stream
#include <stdio.h>
int main() {
char buffer[100];
printf("Enter your name: ");
fgets(buffer, sizeof(buffer), stdin);
printf("Hello, %s", buffer);
return 0;
}
Stream Buffering Mechanism
Streams in C are typically buffered, which means data is collected in memory before being processed, improving I/O performance.
LabEx Tip
At LabEx, we recommend understanding stream basics thoroughly before advanced input handling techniques.