Input Buffer 输入缓冲区
1. Basic Concepts
1.1 What is an Input Buffer?
-
Definition: A temporary storage area in memory that holds input data before it's processed by the program
-
Purpose: Improves I/O efficiency by handling data in chunks rather than character-by-character
1.2 Buffer Types
Three types of buffering:
1. Fully Buffered (全缓冲) - Data flushed when buffer is full
2. Line Buffered (行缓冲) - Data flushed when newline encountered
3. Unbuffered (无缓冲) - Immediate I/O operation
2. How Input Buffer Works
Data Flow
3. Common Buffer Problems 常见缓冲区问题
3.1 Newline Character Issues 换行符问题
// Problem: Mixing numeric and character input
int age;
char grade;
printf("Enter age: ");
scanf("%d", &age); // Input: 18\n
printf("Enter grade: ");
scanf("%c", &grade); // Reads '\n' instead of waiting
3.2 Buffer Overflow 缓冲区溢出
3.3 Partial Read Issues 部分读取问题
4. Solutions and Best Practices 解决方案
Clearing Input Buffer
// Method 1: Simple buffer clearing function
void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
// Method 2: Using space before %c
scanf(" %c", &ch); // Space skips whitespace characters