Scansets & Advanced scanf Patterns in C
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.
Classes in brackets
%[0-9] takes digits, %[a-zA-Z] letters, %[^,] everything-but-comma — patterns, not fixed types.
Cap every scanset
%15[^\n] into char[16] — unbounded scansets overflow exactly like bare %s.
Suppress with *
%*c skips one character (a delimiter), %*[^:] skips a whole field — parse-and-discard in one expression.
Check the count
Assignments counted the same way — short counts mean malformed input, handled, not hoped away.
#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;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.