0Pricing
C Academy · Lesson

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

  1. printf Format Specifiers
  2. scanf and Input
  3. Width, Precision, Flags
  4. Common I/O Pitfalls
← Back to C Academy