c - Is snprintf(NULL,0,...); behavior standardized? -
on linux returns number of characters printed.
is standardized behavior?
yes.
from 7.21.6.5 snprintf function, n1570 (c11 draft):
the snprintf function equivalent fprintf, except output written array (speciļ¬ed argument s) rather stream. if n zero, nothing written, , s may null pointer. otherwise, output characters beyond n-1st discarded rather being written array, , null character written @ end of characters written array. if copying takes place between objects overlap, behavior undefined.
it's useful method find length of unknown data can first find necessary length , allocate exact amount of memory. typical use case is:
char *p; int len = snprintf(0, 0, "%s %s some_long_string_here_", str1, str2); p = malloc(len + 1); snprintf(p, len + 1, "%s %s some_long_string_here", str1, str2);
Comments
Post a Comment