sprintf

sprintf(string $format, mixed ...$values): string

Builds a formatted string from a format string and arguments.

C C23

C23
#include <stdio.h>
int main(void) {
    // printf formats and prints directly (sprintf writes into a char buffer)
    printf("Hello %s, you have %d messages\n", "Alice", 3);
    printf("ID: %05d, price: %.2f\n", 42, 9.5); // zero-pad + fixed decimals
    return 0;
}
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

C++ C++26

C++26
#include <cstdio>
int main() {
    // snprintf formats into a buffer; printf prints directly
    std::printf("Hello %s, you have %d messages\n", "Alice", 3);
    std::printf("ID: %05d, price: %.2f\n", 42, 9.5); // zero-pad + fixed decimals
    return 0;
}
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

C# v11.0.100

v11.0.100
var name = "Alice";
var count = 3;
Console.WriteLine($"Hello {name}, you have {count} messages");
Console.WriteLine(string.Format("ID: {0:D5}, price: {1:F2}", 42, 9.5)); // composite formatting
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
    fmt.Printf("Hello %s, you have %d messages\n", "Alice", 3)

	fmt.Printf("ID: %05d, price: %.2f\n", 42, 9.5) // zero-pad + fixed decimals
}
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        System.out.printf("Hello %s, you have %d messages%n", "Alice", 3);
        System.out.printf("ID: %05d, price: %.2f%n", 42, 9.5); // zero-pad + fixed decimals
    }
}
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

JavaScript v26.13.1

v26.13.1
const name = "Alice";
const count = 3;
console.log(`Hello ${name}, you have ${count} messages`);
// padStart/toFixed are the JS route to width/precision
console.log(`ID: ${String(42).padStart(5, "0")}, price: ${(9.5).toFixed(2)}`)
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

PHP v8.6.0

v8.6.0
echo sprintf("Hello %s, you have %d messages", "Alice", 3), "\n";
// width/precision specifiers: %05d zero-pads, %.2f fixes decimals
echo sprintf("ID: %05d, price: %.2f", 42, 9.5);
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

Python v3.15.2

v3.15.2
print("Hello %s, you have %d messages" % ("Alice", 3))
print(f"ID: {42:05d}, price: {9.5:.2f}") # format-spec mini language
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

Rust v1.99.1

v1.99.1
fn main() {
    // the format! macro builds a format string, {} is the placeholder; println! prints directly
    let msg = format!("Hello {}, you have {} messages", "Alice", 3);
    println!("{}", msg);

    println!("ID: {:05}, price: {:.2}", 42, 9.5); // zero-pad + fixed decimals
}
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

TypeScript v26.13.1

v26.13.1
const name: string = "Alice";
const count: number = 3;
console.log(`Hello ${name}, you have ${count} messages`);
console.log(`ID: ${String(42).padStart(5, "0")}, price: ${(9.5).toFixed(2)}`); // padStart/toFixed
Output
Hello Alice, you have 3 messages
ID: 00042, price: 9.50

Language differences

C: formats and prints directly with printf (sprintf writes into a buffer).

Go: Printf prints it directly.

Java: Printf prints it directly.

JavaScript: uses template literals (backticks + ${}).

PHP: sprintf returns the string.

Python: offers three styles — %, .format(), and f-strings.

Rust: builds formatted strings with the format! macro ({} placeholders); println! prints directly.