Common I/O Pitfalls
Buffer and newline issues.
Common I/O Bugs
Input and output in C have a few classic traps. Knowing them saves hours of debugging.
#include <stdio.h>
int main(void) {
printf("Let us study I/O pitfalls\n");
return 0;
}The Leftover Newline
After scanf("%d", ...) the Enter key leaves a \n in the buffer. A later %c reads that newline instead of real input.
#include <stdio.h>
int main(void) {
char c = 'A';
printf("Use a space before %%c to skip the leftover newline\n");
printf("c = %c\n", c);
return 0;
}All lessons in this course
- printf Format Specifiers
- scanf and Input
- Width, Precision, Flags
- Common I/O Pitfalls