Dec 27, 2021 · const char * fmt = "sqrt (2) = %f"; int sz = snprintf (NULL, 0, fmt, sqrt(2)); char buf [ sz + 1]; // note +1 for terminating null byte snprintf ( buf, sizeof buf, fmt, sqrt(2)); snprintf_s, just like snprintf, but unlike sprintf_s, will truncate the output to fit in bufsz-1 .