File: snprintf.3

package info (click to toggle)
manpages 6.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,812 kB
  • sloc: sh: 853; python: 227; perl: 191; makefile: 29; lisp: 22
file content (249 lines) | stat: -rw-r--r-- 5,540 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
'\" t
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: GPL-2.0-or-later
.\"
.TH snprintf 3 2025-12-07 "Linux man-pages (unreleased)"
.SH NAME
snprintf,
vsnprintf
\-
string print formatted
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #include <stdio.h>
.P
.BR "int snprintf(" "size_t size;"
.BI "            char " str "[restrict " size "], size_t " size ,
.BI "            const char *restrict " format ", ...);"
.BR "int vsnprintf(" "size_t size;"
.BI "            char " str "[restrict " size "], size_t " size ,
.BI "            const char *restrict " format ", va_list " ap );
.fi
.P
.RS -4
Feature Test Macro Requirements for glibc (see
.BR feature_test_macros (7)):
.RE
.P
.BR snprintf (),
.BR vsnprintf ():
.nf
    _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE
        || /* glibc <= 2.19: */ _BSD_SOURCE
.fi
.SH DESCRIPTION
These functions are similar to
.BR printf (3),
except that they write to the character string
.I str
instead of a stream.
.P
The functions
.BR snprintf ()
and
.BR vsnprintf ()
write at most
.I size
bytes (including the terminating null byte (\[aq]\[rs]0\[aq])) to
.IR str .
.P
.BR vsnprintf ()
is equivalent to
.BR snprintf (),
except that it is called with a
.I va_list
instead of a variable number of arguments.
This function does not call the
.I va_end
macro.
Because it invokes the
.I va_arg
macro, the value of
.I ap
is undefined after the call.
See
.BR stdarg (3).
.P
C99 and POSIX.1-2001 specify that the results are undefined if a call to
.BR snprintf ()
or
.BR vsnprintf ()
would cause copying to take place between objects that overlap
(e.g., if the target string array and one of the supplied input arguments
refer to the same buffer).
See CAVEATS.
.SS Format of the format string
See
.BR printf (3).
.SH RETURN VALUE
Upon successful return, these functions return the number of bytes
printed (excluding the null byte used to end output to strings).
.P
The functions
.BR snprintf ()
and
.BR vsnprintf ()
do not write more than
.I size
bytes (including the terminating null byte (\[aq]\[rs]0\[aq])).
If the output was truncated due to this limit, then the return value
is the number of characters (excluding the terminating null byte)
which would have been written to the final string if enough space
had been available.
Thus, a return value of
.I size
or more means that the output was truncated.
(See also below under CAVEATS.)
.P
On error,
a negative value is returned,
and
.I errno
is set to indicate the error.
.SH ERRORS
See
.BR printf (3).
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface	Attribute	Value
T{
.na
.nh
.BR snprintf (),
.BR vsnprintf ()
T}	Thread safety	MT-Safe locale
.TE
.SH STANDARDS
C11, POSIX.1-2008.
.SH HISTORY
SUSv2, C99, POSIX.1-2001.
.IP
Concerning the return value,
SUSv2 and C99 contradict each other: when
.BR snprintf ()
is called with
.IR size =0
then SUSv2 stipulates an unspecified return value less than 1,
while C99 allows
.I str
to be NULL in this case, and gives the return value (as always)
as the number of characters that would have been written in case
the output string has been large enough.
POSIX.1-2001 and later align their specification of
.BR snprintf ()
with C99.
.SH CAVEATS
Some programs imprudently rely on code such as the following
.P
.in +4n
.EX
snprintf(buf, countof(buf), "%s some further text", buf);
.EE
.in
.P
to append text to
.IR buf .
However, the standards explicitly note that the results are undefined
if source and destination buffers overlap when calling
.BR snprintf ()
and
.BR vsnprintf ().
.\" http://sourceware.org/bugzilla/show_bug.cgi?id=7075
Depending on the version of
.BR gcc (1)
used, and the compiler options employed, calls such as the above will
.B not
produce the expected results.
.P
The glibc implementation of the functions
.BR snprintf ()
and
.BR vsnprintf ()
conforms to the C99 standard, that is, behaves as described above,
since glibc 2.1.
Until glibc 2.0.6, they would return \-1
when the output was truncated.
.\" .SH HISTORY
.\" UNIX V7 defines the three routines
.\" .BR sprintf ().
.\" 2.11BSD has
.\" .BR vsprintf ().
.\" 4.4BSD introduces the functions
.\" .BR snprintf ()
.\" and
.\" .BR vsnprintf ().
.\" FreeBSD also has functions
.\" .BR asprintf ()
.\" and
.\" .BR vasprintf (),
.\" that allocate a buffer large enough for
.\" .BR sprintf ().
.SH BUGS
See
.BR printf (3).
.SH EXAMPLES
To allocate a sufficiently large string and print into it
(code correct for both glibc 2.0 and glibc 2.1):
.P
.EX
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
\&
char *
make_message(const char *fmt, ...)
{
    int n = 0;
    size_t size = 0;
    char *p = NULL;
    va_list ap;
\&
    /* Determine required size.  */
\&
    va_start(ap, fmt);
    n = vsnprintf(p, size, fmt, ap);
    va_end(ap);
\&
    if (n < 0)
        return NULL;
\&
    size = (size_t) n + 1;      /* One extra byte for \[aq]\[rs]0\[aq] */
    p = malloc(size);
    if (p == NULL)
        return NULL;
\&
    va_start(ap, fmt);
    n = vsnprintf(p, size, fmt, ap);
    va_end(ap);
\&
    if (n < 0) {
        free(p);
        return NULL;
    }
\&
    return p;
}
.EE
.P
If truncation occurs in glibc versions prior to glibc 2.0.6,
this is treated as an error instead of being handled gracefully.
.SH SEE ALSO
.BR printf (1),
.BR asprintf (3),
.BR printf (3),
.BR puts (3),
.BR scanf (3),
.BR setlocale (3),
.BR strfromd (3),
.BR wcrtomb (3),
.BR wprintf (3),
.BR locale (5)