absl::SNPrintF

Writes to a sized buffer given a format string and zero or more arguments.

Synopsis

Declared in <absl/strings/str_format.h>

template<typename... Args>
int
SNPrintF(
    char* output,
    std::size_t size,
    FormatSpec<Args...> const& format,
    Args const&... args);

Description

This function is functionally equivalent to std::snprintf() (and type-safe); prefer absl::SNPrintF() over std::snprintf().

In particular, a successful call to absl::SNPrintF() writes at most size bytes of the formatted output to output, including a NUL-terminator, and returns the number of bytes that would have been written if truncation did not occur. In the event of an error, a negative value is returned and errno is set.

Example:

std::string_view s = "Ulaanbaatar"; char output[128]; absl::SNPrintF(output, sizeof(output), "The capital of Mongolia is %s", s);

Post-condition: output == "The capital of Mongolia is Ulaanbaatar"

Return Value

The number of bytes that would have been written, or a negative value on error.

Parameters

NameDescription
outputThe buffer to write to.
sizeThe size of the buffer, in bytes.
formatThe format string.
argsThe arguments substituted into the format string.