printf / vprintf / vfprintf / fprintf / vsprintf
printf(string $format, mixed ...$values): int — vprintf(string $format, array $values): int — fprintf(resource $stream, string $format, mixed ...$values): int — vfprintf(resource $stream, string $format, array $values): int — vsprintf(string $format, array $values): string
The formatted-output family: printf/fprintf write straight to a stream and return the byte count (contrast sprintf, which returns the string); the v* prefix takes an ARRAY of arguments (va_list/array instead of a variadic list).
C C23
/* printf(3) family: printf / fprintf / vfprintf - the C originals.
v* = va_list argument vector; f* = explicit stream target. */
#include <stdarg.h>
#include <stdio.h>
static void log_line(FILE *fp, const char *fmt, ...) { /* v-prefix semantics */
va_list ap;
va_start(ap, fmt);
vfprintf(fp, fmt, ap); /* the v* core */
va_end(ap);
}
int main(void) {
int n = printf("%s|%05.2f\n", "pi", 3.14159); /* returns byte count */
printf("printf wrote %d bytes\n", n);
log_line(stdout, "[%05.2f] %s\n", 3.14159, "pi");
fprintf(stdout, "fprintf targets a stream\n");
return 0;
}
pi|03.14 printf wrote 9 bytes [03.14] pi fprintf targets a stream
C++ C++26
// The C family via <cstdio>, plus std::format (C++20) as the modern
// sprintf twin - type-safe placeholder list instead of format specifiers.
#include <cstdio>
#include <format>
#include <iostream>
int main() {
int n = std::printf("%s|%05.2f\n", "pi", 3.14159);
std::printf("printf wrote %d bytes\n", n);
std::cout << std::format("[{:05.2f}] {}", 3.14159, "pi") << "\n"; // sprintf twin
std::fprintf(stdout, "fprintf targets a stream\n");
}
pi|03.14 printf wrote 9 bytes [03.14] pi fprintf targets a stream
C# v11.0.100
// Console.Write streams; string.Format RETURNS the string (sprintf twin);
// params object[] covers the v-prefix array form.
Console.Write("{0}|{1:00.00}\n", "pi", 3.14159);
var formatted = string.Format("[{0:00.00}] {1}", 3.14159, "pi"); // sprintf twin
Console.WriteLine("formatted: " + formatted);
object[] fmtArgs = { "alice", 92 }; // the v-prefix array form
Console.WriteLine(string.Format("{0} has {1} points", fmtArgs));
using (var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true }) {
sw.Write("fprintf-style: {0} points\n", 92); // explicit stream
}
pi|03.14 formatted: [03.14] pi alice has 92 points fprintf-style: 92 points
Go v1.27.4
// fmt.Fprintf writes to any io.Writer - the direct twin of fprintf;
// Printf = stdout, Sprintf returns. Go has no v-prefix split: a slice
// with ... covers the array-of-args form.
package main
import (
"fmt"
"os"
)
func main() {
n, _ := fmt.Fprintf(os.Stdout, "%s|%05.2f\n", "pi", 3.14159)
fmt.Printf("fprintf wrote %d bytes\n", n)
fmt.Println(fmt.Sprintf("[%05.2f] %s", 3.14159, "pi")) // sprintf twin
args := []any{"alice", 92} // the v-prefix array form
fmt.Printf("%s has %d points\n", args...)
}
pi|03.14 fprintf wrote 9 bytes [03.14] pi alice has 92 points
Java v27.0.1
// System.out.printf writes to the stream and returns THAT (not a count!);
// String.format is the sprintf twin; an Object[] covers the v* array form.
public class Main {
public static void main(String[] args) {
System.out.printf("%s|%05.2f%n", "pi", 3.14159);
Object[] fmtArgs = {"alice", 92}; // the v-prefix array form
System.out.println(String.format("%s has %d points", fmtArgs));
System.out.println(String.format("[%05.2f] %s", 3.14159, "pi"));
System.out.print("printf is stream-fluent\n");
}
}
pi|03.14 alice has 92 points [03.14] pi printf is stream-fluent
JavaScript v26.13.1
// No printf in the language core; Node's util.format covers the %s/%d
// placeholder style (no width/precision specifiers - pad manually).
const util = require("node:util");
const num5_2 = (3.14159).toFixed(2).padStart(5, "0"); // "03.14": manual 05.2f
console.log(util.format("%s|%s", "pi", num5_2));
const formatted = util.format("[%s] %s", num5_2, "pi"); // sprintf-style: returns
console.log(formatted);
const args = ["alice", 92]; // the v-prefix array form
console.log(util.format("%s has %d points", ...args));
console.log("fprintf-style: 92 points"); // process.stdout.write streams it
pi|03.14 [03.14] pi alice has 92 points fprintf-style: 92 points
PHP v8.6.0
// printf family: printf/fprintf write to a STREAM and return the byte
// count; sprintf returns the string. The v* variants take an ARRAY of
// arguments instead of a variadic list.
$n = printf("%s|%05.2f\n", "pi", 3.14159);
echo "printf wrote {$n} bytes\n";
echo vsprintf("%s has %d points", ["alice", 92]), "\n";
$fp = fopen("php://temp", "r+");
fprintf($fp, "[%05.2f] %s", 3.14159, "pi"); // stream target
rewind($fp);
echo "stream readback: ", fgets($fp), "\n";
echo sprintf("sprintf just returns: %d%%\n", 100);
pi|03.14 printf wrote 9 bytes alice has 92 points stream readback: [03.14] pi sprintf just returns: 100%
Python v3.15.2
# The % operator is printf's direct descendant; % formatting RETURNS the
# string (sprintf twin); sys.stdout.write is the stream form.
import sys
print("%s|%05.2f" % ("pi", 3.14159))
print("[%05.2f] %s" % (3.14159, "pi"))
args = ("alice", 92) # the v-prefix array form
print("%s has %d points" % args)
sys.stdout.write("fprintf-style: %d points\n" % 92)
pi|03.14 [03.14] pi alice has 92 points fprintf-style: 92 points
Rust v1.99.1
// format! is the sprintf twin (returns String); println!/print! are the
// stdout writers. Width/zero-pad goes in the format spec {:05.2}.
fn main() {
let s = format!("{}|{:05.2}", "pi", 3.14159); // sprintf-style: returns
println!("{}", s);
println!("[{:05.2}] {}", 3.14159, "pi"); // direct stdout macro
let args = ("alice", 92u32); // tuple ≈ the v-prefix array form
println!("{} has {} points", args.0, args.1);
}
pi|03.14 [03.14] pi alice has 92 points
TypeScript v26.13.1
// Same as JavaScript: util.format placeholders + manual width/precision.
import * as util from "node:util";
const num5_2: string = (3.14159).toFixed(2).padStart(5, "0");
console.log(util.format("%s|%s", "pi", num5_2));
const formatted: string = util.format("[%s] %s", num5_2, "pi");
console.log(formatted);
const args: Array<string | number> = ["alice", 92];
console.log(util.format("%s has %d points", ...args));
pi|03.14 [03.14] pi alice has 92 points
Language differences
C: printf/fprintf/vfprintf are the originals - PHP’s family inherits C semantics verbatim (%05.2f, %d, %% all isomorphic); the C block hand-writes a va_list wrapper to demonstrate the v-prefix. C++: same as C (cstdio) plus std::format (C++20) as the modern sprintf twin ({:05.2f} type-safe placeholders). C#: Console.Write/sw.Write stream; string.Format returns the string; the {1:00.00} format zero-pads the integer part to align with PHP %05.2f. Go: fmt.Fprintf(w, ...) returns (n, err) - the task list’s flagged Go counterpart; the fmt verbs F/P/S map one-to-one onto the PHP five (F-to-fprintf, P-to-printf, S-to-sprintf), and args... slice expansion is the v semantics. Java: System.out.printf returns the PrintStream itself (not a byte count - unlike PHP/C returning int); String.format is sprintf; %n is the platform newline vs `
. JavaScript/TypeScript: no printf - Node util.format offers only %s/%d placeholders with **no width/precision specifiers**, so 05.2f is hand-rolled via toFixed(2).padStart(5, "0"). PHP: family division of labor - printf/fprintf/vprintf/vfprintf/fprintf **write a stream and return the byte count**, sprintf/vsprintf **return the string**; v* take an array argument (vsprintf is the template-rendering workhorse); %% emits a literal %; since 8.0 a zero-argument call throws ValueError (previously E_WARNING); the fprintf target is any stream resource (fopen file, php://temp in-memory stream, the STDERR constant). Python: the % operator is printf’s direct descendant ("%s|%05.2f" % args returns the string - sprintf semantics); sys.stdout.write streams. Rust: format! returns the string (sprintf twin), println! writes stdout; {:05.2} aligns the spec; no separate fprintf stream family - the write! macro over io::Write` takes that role. Teaching anchor: five functions = the Cartesian product of two dimensions (destination: stream vs returned string, and argument shape: variadic vs array); the Go fmt three-verb to PHP five-function mapping is this entry’s core teaching chart.