Learn C
#include "ioformatting/scansets.h"

Scansets & Advanced scanf Patterns in C

#concept

Scansets (%[...]) match character classes instead of fixed types: %[0-9] reads digits, %[^,] reads up to a comma, %[^\n] grabs whole lines — filling scanf's biggest gap (whitespace-delimited %s) without leaving the function. Width caps (%15[^,]) keep them bounded, and %*c-style suppression skips unwanted fields.

Scansets still write through pointers into caller buffers (size discipline unchanged) and still return match counts to check. They shine for delimited records and constrained tokens; full-line-plus-parse remains the fgets+sscanf pattern with scansets inside the sscanf.

#how it works
1

Classes in brackets

%[0-9] takes digits, %[a-zA-Z] letters, %[^,] everything-but-comma — patterns, not fixed types.

2

Cap every scanset

%15[^\n] into char[16] — unbounded scansets overflow exactly like bare %s.

3

Suppress with *

%*c skips one character (a delimiter), %*[^:] skips a whole field — parse-and-discard in one expression.

4

Check the count

Assignments counted the same way — short counts mean malformed input, handled, not hoped away.

#example
#include <stdio.h>
 
int main(void) {
char first[16] = {0};
char last[16] = {0};
if (sscanf("ada,lovelace", "%15[^,],%15s", first, last) == 2) {
printf("%s %s\n", first, last);
}
return 0;
}
Output
ada lovelace
1%15[^,] reads up to (not including) the comma, bounded to the buffer — ada lands in first.
2The literal comma in the format consumes the delimiter; %15s takes lovelace.
3Both assigned (== 2 verified), so ada lovelace prints — delimited parsing without manual splitting.
#use cases
CSV-ish field splitting with %[^,]-style patterns
Constrained tokens (digits, identifiers) matched by class
Skipping separators and junk fields with suppression
Bounded scansets everywhere bare %s used to overflow

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.