SDL_snprintf(3)

πŸ’… SDL_snprintf(3) β€” the SDL3 functions era

okay bestie, welcome to the <SDL3/SDL_stdinc.h> chapter of your life, this one's called SDL_snprintf and it's literally snprintf() but make it SDL β€” same energy, except it does not need access to a C runtime. no runtime? no problem. periodt. πŸ’–


✨ SYNOPSIS ✨

#include <SDL3/SDL_stdinc.h>

int SDL_snprintf(char *text, size_t maxlen, const char *fmt, ...);

🌸 DESCRIPTION 🌸

So what it does is format a string of up to maxlen-1 bytes, converting each '%' item using the values you hand it through the variable arguments. cute, right?

Now here's the tea, because some C runtimes are messy about too-large strings: this one null-terminates the output, by treating that null-terminator as part of the maxlen count. iconic behaviour, honestly. πŸ™ˆ but heads up β€” if maxlen is zero, then no bytes get written at all. zero. nada. nothing. πŸ’€

The return value is the number of _bytes_ (not _characters_, bestie, bytes) that should be written, and that count excludes the null-terminator character. so if it hands you a number >= maxlen, that means your output string got truncated 😭 and a negative return value means an error happened. fr fr.

⚠️ Referencing the output string's pointer with a format item is undefined behavior. don't do it. not even a little. no cap.

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • text β€” the buffer you're writing the string into. it must not be NULL. 🫢
  • maxlen β€” the maximum bytes to write, including the null-terminator. yes, the terminator counts, it's part of the squad.
  • fmt β€” a printf-style format string. must not be NULL either. ⭐
  • ... β€” a whole list of values to be used with the format string. go off.

πŸ‘‘ RETURN VALUE πŸ‘‘

Returns the number of bytes that should be written, not counting the null-terminator char β€” or a negative value on error. that's it, that's the vibe check. πŸ’…


🧚 THREAD SAFETY 🧚

It is safe to call this function from any thread. every thread is welcome at this party. 🌈


πŸ¦‹ AVAILABILITY πŸ¦‹

This function is available since SDL 3.2.0. so if you're on something older… it's giving not yet, bestie. πŸ’œ


made with love by Simple Directmedia Layer, shipping as <b>SDL 3.4.18</b>, and this lil page is filed under SDL_snprintf(3). stay slay, keep your buffers null-terminated, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from