Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdio.h

C sscanf() and fscanf() functions

Reference for C sscanf() and fscanf(): parse values from a string or a FILE stream, formats, the %n trick, return values and safe line-by-line parsing.

sscanf() reads values out of a string you already have in memory, and fscanf() reads them from any FILE * stream. Both use the same format language as scanf(); only the source differs. scanf(fmt, ...) is the same as fscanf(stdin, fmt, ...).

The best-known pattern for robust input is to read a whole line with fgets() and then parse it with sscanf(). If the parse fails you still have the original line to report, and the stream stays in a clean state, which solves most of the scanf pitfalls.

C
#include <stdio.h>

int sscanf(const char *restrict s, const char *restrict format, ...);
int fscanf(FILE *restrict stream, const char *restrict format, ...);

Try it

C Parsing a record from a string

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Reading a table with fscanf, and checking for trailing text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int sscanf(const char *restrict s, const char *restrict format, ...);
int fscanf(FILE *restrict stream, const char *restrict format, ...);
Name Description
s The null-terminated string to read from. sscanf treats the end of the string like end-of-file.
stream An open, readable FILE *.
format, ... Format and result pointers, exactly as for scanf.

Returns: the number of items assigned, or EOF if the input ended (or a read error happened) before the first conversion.

Errors / undefined behavior: wrong pointer types, missing arguments and unbounded %s into a small array are undefined behavior. sscanf on a string that is not terminated is undefined as well. Source and destination of a sscanf must not overlap.

Usage notes

  • Return value. 3 means three fields were stored; 0 means the first conversion failed (a mismatch); EOF means no input at all (an empty string for sscanf). Compare with the exact expected count.
  • sscanf does not remember its position. Each call starts at the beginning of the string. To parse a sequence, use %n to learn how many characters were consumed and advance your pointer: p += used.
  • Detecting trailing junk. sscanf("12abc", "%d", &n) returns 1. To demand that the entire string was a number, add %n after the conversion and test text[used] == '\0', or use strtol(), which gives better error information.
  • Overflow is undefined. sscanf("99999999999", "%d", &n) has undefined behavior; strtol reports ERANGE instead.
  • Widths protect buffers. Always bound %s and %[: %15s for char[16].
  • %[^,] reads up to a comma, which is how you split simple comma-separated fields; an empty field makes the match fail (a %[ conversion needs at least one character).
  • fscanf loops end on mismatch too. A while (fscanf(...) == 2) loop stops at a bad row, not only at end of file, so check feof(f) afterwards if the difference matters (see feof and ferror).
  • Line oriented files are better handled with fgets plus sscanf, since fscanf lets %s cross line boundaries (newlines are whitespace).
  • Speed. sscanf is convenient, not fast; it parses the format string on every call. For large inputs, write a tokenizer or use strtol/strtod.