1STRLEN(3P) POSIX Programmer's Manual STRLEN(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 strlen, strnlen — get length of fixed size string
13
15 #include <string.h>
16
17 size_t strlen(const char *s);
18 size_t strnlen(const char *s, size_t maxlen);
19
21 For strlen(): The functionality described on this reference page is
22 aligned with the ISO C standard. Any conflict between the requirements
23 described here and the ISO C standard is unintentional. This volume of
24 POSIX.1‐2017 defers to the ISO C standard.
25
26 The strlen() function shall compute the number of bytes in the string
27 to which s points, not including the terminating NUL character.
28
29 The strnlen() function shall compute the smaller of the number of bytes
30 in the array to which s points, not including any terminating NUL char‐
31 acter, or the value of the maxlen argument. The strnlen() function
32 shall never examine more than maxlen bytes of the array pointed to by
33 s.
34
36 The strlen() function shall return the length of s; no return value
37 shall be reserved to indicate an error.
38
39 The strnlen() function shall return the number of bytes preceding the
40 first null byte in the array to which s points, if s contains a null
41 byte within the first maxlen bytes; otherwise, it shall return maxlen.
42
44 No errors are defined.
45
46 The following sections are informative.
47
49 Getting String Lengths
50 The following example sets the maximum length of key and data by using
51 strlen() to get the lengths of those strings.
52
53
54 #include <string.h>
55 ...
56 struct element {
57 char *key;
58 char *data;
59 };
60 ...
61 char *key, *data;
62 int len;
63
64 *keylength = *datalength = 0;
65 ...
66 if ((len = strlen(key)) > *keylength)
67 *keylength = len;
68 if ((len = strlen(data)) > *datalength)
69 *datalength = len;
70 ...
71
73 None.
74
76 None.
77
79 None.
80
82 wcslen()
83
84 The Base Definitions volume of POSIX.1‐2017, <string.h>
85
87 Portions of this text are reprinted and reproduced in electronic form
88 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
89 table Operating System Interface (POSIX), The Open Group Base Specifi‐
90 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
91 Electrical and Electronics Engineers, Inc and The Open Group. In the
92 event of any discrepancy between this version and the original IEEE and
93 The Open Group Standard, the original IEEE and The Open Group Standard
94 is the referee document. The original Standard can be obtained online
95 at http://www.opengroup.org/unix/online.html .
96
97 Any typographical or formatting errors that appear in this page are
98 most likely to have been introduced during the conversion of the source
99 files to man page format. To report such errors, see https://www.ker‐
100 nel.org/doc/man-pages/reporting_bugs.html .
101
102
103
104IEEE/The Open Group 2017 STRLEN(3P)