Skip to content

Instantly share code, notes, and snippets.

@sgk
Last active December 11, 2015 19:18
Show Gist options
  • Save sgk/4647146 to your computer and use it in GitHub Desktop.
Save sgk/4647146 to your computer and use it in GitHub Desktop.
Compute the length printf would print.
#include <stdio.h>
#include <stdarg.h>
/* returns the length printf would print. */
int
printflen(const char* format, ...) {
va_list ap;
char buf[1];
int len;
va_start(ap, format);
len = vsnprintf(buf, sizeof (buf), format, ap);
va_end(ap);
return len;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment