Skip to main content

Reading Input โ€” and Parsing Text Safely

beginner12 min readLesson 9 of 148

How scanf works interactively, why it is dangerous without width limits, and the sscanf pattern graded in this course.

Interactive input: scanf, carefully

scanf("%d", &n) reads whitespace-separated tokens from standard input. Note the & โ€” scanf needs the address of n to store into (module 11 explains why). Always check its return value: the number of items successfully read.

int n;
if (scanf("%d", &n) == 1) {
    printf("read %d\n", n);
}

Danger: scanf("%s", buf) with no width can overflow buf with long input โ€” a buffer overflow, the classic C vulnerability. With a width it is bounded: scanf("%63s", buf) reads at most 63 characters plus the terminator into a 64-byte buffer.

The graded pattern: sscanf

This course's challenges run non-interactively, so graded input handling uses sscanf โ€” the same parsing engine, reading from a string instead of stdin:

const char* line = "Alice 42";
char name[32]; int age = 0;
int got = sscanf(line, "%31s %d", name, &age);
// got == 2, name == "Alice", age == 42

The skill being graded is identical to reading user input: describe the shape of the line, extract the fields, verify the count.

Now practice

Parse the LineExtract typed fields from text lines with sscanf โ€” the graded form of reading input.2 challenges ยท ยท ~14 min