sscanf

sscanf(string $string, string $format, mixed &...$vars): array|int|null

The input counterpart of printf(): parses fields out of a string by a format; the two-argument form returns the parsed array, while the by-reference form returns the number of fields assigned.

C C23

C23
#include <stdio.h>

int main(void) {
    char name[32], status[32];
    int id;
    // sscanf is the input counterpart of printf: returns the converted-field count
    int n = sscanf("order:42:ship", "%31[^:]:%d:%31s", name, &id, status);
    printf("%d\n", n);
    printf("%s %d %s\n", name, id, status);
    int m = sscanf("abc", "%d", &id); // matching failure: zero conversions
    printf("%d\n", m);               // 0
    int e = sscanf("", "%d", &id);    // input exhausted before any conversion
    printf("%d\n", e);               // EOF == -1
    return 0;
}
Output
3
order 42 ship
0
-1

C++ C++26

C++26
#include <cstdio>

int main() {
    char name[32], status[32];
    int id;
    // C++ keeps the C stdio family: same sscanf semantics
    int n = sscanf("order:42:ship", "%31[^:]:%d:%31s", name, &id, status);
    std::printf("%d\n", n);
    std::printf("%s %d %s\n", name, id, status);
    int m = sscanf("abc", "%d", &id);
    std::printf("%d\n", m); // 0: matching failure
    int e = sscanf("", "%d", &id);
    std::printf("%d\n", e); // EOF == -1
    return 0;
}
Output
3
order 42 ship
0
-1

C# v11.0.100

v11.0.100
// C# has no scanf: Split + Parse is the idiomatic hand-rolled equivalent
var parts = "order:42:ship".Split(':');
int id = int.Parse(parts[1]);
Console.WriteLine(parts[0] + " " + id + " " + parts[2]);
Console.WriteLine(parts.Length);              // 3 fields parsed
Console.WriteLine(int.TryParse("abc", out _)); // False: parse fails
Output
order 42 ship
3
False

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
)

func main() {
	// fmt.Sscanf is native — but has NO %[...] scansets; use whitespace-separated formats
	var name, status string
	var id int
	n, _ := fmt.Sscanf("order 42 ship", "%s %d %s", &name, &id, &status)
	fmt.Println(n) // 3 fields parsed
	fmt.Println(name, id, status)
	var bad int
	m, err2 := fmt.Sscanf("abc", "%d", &bad)
	fmt.Println(m, err2 != nil) // 0 true: parse fails
}
Output
3
order 42 ship
0 true

Java v27.0.1

v27.0.1
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // no scanf format string: Scanner reads typed tokens split by a delimiter
        Scanner sc = new Scanner("order 42 ship");
        String name = sc.next();
        int id = sc.nextInt();
        String status = sc.next();
        System.out.println(name + " " + id + " " + status);
        Scanner bad = new Scanner("abc");
        System.out.println(bad.hasNextInt()); // false: parse fails
    }
}
Output
order 42 ship
false

JavaScript v26.13.1

v26.13.1
// JavaScript has no sscanf: regex capture groups are the hand-rolled equivalent
const m = "order:42:ship".match(/^([^:]+):(\d+):([a-z]+)$/);
console.log(m[1] + " " + Number(m[2]) + " " + m[3]);
console.log(m.length - 1);       // 3 captured fields
console.log(/^\d+/.test("abc")); // false: parse fails
Output
order 42 ship
3
false

PHP v8.6.0

v8.6.0
$n = sscanf('order:42:ship', '%[^:]:%d:%s', $name, $id, $status);
echo $n, "\n"; // 3 fields parsed
echo $name, ' ', $id, ' ', $status, "\n";
$m = sscanf('abc', '%d'); // nothing convertible: the field comes back NULL
var_dump($m[0]);
Output
3
order 42 ship
NULL

Python v3.15.2

v3.15.2
import re

# Python has no sscanf: regex match (or split + int()) is the idiomatic equivalent
m = re.match(r"([^:]+):(\d+):([a-z]+)", "order:42:ship")
if m:
    print(m.group(1), int(m.group(2)), m.group(3))
    print(len(m.groups()))        # 3 captured fields
print(bool(re.match(r"\d+", "abc")))  # False: parse fails
Output
order 42 ship
3
False

Rust v1.99.1

v1.99.1
// Rust has no scanf: split + parse is the hand-rolled equivalent
fn main() {
    let parts: Vec<&str> = "order:42:ship".split(':').collect();
    let id: i32 = parts[1].parse().unwrap();
    println!("{} {} {}", parts[0], id, parts[2]);
    println!("{}", parts.len());               // 3 fields parsed
    println!("{}", "abc".parse::<i32>().is_err()); // true: parse fails
}
Output
order 42 ship
3
true

TypeScript v26.13.1

v26.13.1
// TypeScript has no sscanf either: regex capture groups stand in
const m: RegExpMatchArray = "order:42:ship".match(/^([^:]+):(\d+):([a-z]+)$/)!;
console.log(m[1] + " " + Number(m[2]) + " " + m[3]);
console.log(m.length - 1);       // 3 captured fields
console.log(/^\d+/.test("abc")); // false: parse fails
Output
order 42 ship
3
false

Language differences

PHP: sscanf parses by format (the manual calls it the input analog of printf()); the two-argument form returns an array, while passing by-reference variables makes it return the assigned-field count; whitespace in the format matches any whitespace in the input (manual, verbatim).

PHP specifics: %s stops at whitespace; the format does not support F/g/G/b, D is decimal, i auto-detects the base, * suppresses assignment — differences from the C scanf family (manual, differences section).

PHP miss semantics: with nothing convertible the field comes back NULL (see sscanf('abc', '%d') yielding [0]=>NULL), unlike C's EOF/-1.

C: same-family prototype: returns the conversion count; a miss has two states — input present but non-matching returns 0, exhausted input (empty string) returns EOF(-1), and the example runs both branches; %31[^:] width caps prevent buffer overflows.

Go: fmt.Sscanf is native but has NO %[...] scansets (the example uses a whitespace-separated format); it returns a count plus an error.

Java: no scanf format string — Scanner takes tokens by delimiter with typed readers like nextInt.

Python: no direct counterpart — idioms are re.match or split plus int(); the struct module unpacks binary data (out of scope here).

JavaScript/TypeScript: no scanf — regex capture groups or split; Rust: no scanf — split(':') + parse(); C#: no scanf — Split + int.Parse/TryParse.

Security: the scanf family has been the vehicle of many format-string exploits (runaway widths, conversion mismatches) — validate untrusted input against a whitelist before parsing.