.\" Hey Emacs! This file is -*- nroff -*- source. .\" (c) 1995 by Jim Van Zandt .\" .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" .\" Added _GNU_SOURCE, aeb, Sat Jul 5 23:10:04 MET 1997 .\" .TH SNPRINTF 3 "16 September 1995" "GNU" "Linux Programmer's Manual" .SH NAME snprintf, vsnprintf \- formatted output conversion .SH SYNOPSIS .B #define _GNU_SOURCE .br .B #include .sp .BI "int snprintf ( char *" str ", size_t " n ", " .br .BI " const char *" format ", ... );" .sp .B #include .sp .BI "int vsnprintf ( char *" str ", size_t " n ", " .br .BI " const char *" format ", va_list " ap " );" .SH DESCRIPTION \fBsnprintf\fP writes output to the string \fIstr\fP, under control of the \fIformat\fP string that specifies how subsequent arguments are converted for output. It is similar to \fBsprintf\fP(3), except that \fIn\fP specifies the maximum number of characters to produce. The trailing null character is counted towards this limit, so you should allocate at least \fIn\fP characters for the string \fIstr\fP. .PP \fBvsnprintf\fP is the equivalent of \fBsnprintf\fP with the variable argument list specified directly as for \fBvprintf\fP. .SH "RETURN VALUE" If the output was truncated, the return value is -1, otherwise it is the number of characters stored, not including the terminating null. .SH EXAMPLES .br Here is an example program which dynamically enlarges its output buffer. .br .sp .nf /* Construct a message describing the value of a variable whose name is NAME and whose value is VALUE. */ char * make_message (char *name, char *value) { /* Guess we need no more than 100 chars of space. */ int size = 100; char *buffer = (char *) xmalloc (size); while (1) { /* Try to print in the allocated space. */ int nchars = snprintf (buffer, size, "value of %s is %s", name, value); /* If that worked, return the string. */ if (nchars > -1) return buffer; /* Else try again with twice as much space. */ size *= 2; buffer = (char *) xrealloc (buffer, size); } } .fi .RE .SH "CONFORMING TO" These are GNU extensions. .SH "SEE ALSO" .BR printf "(3), " sprintf "(3), " vsprintf "(3), " stdarg (3)