STRLEN(3POSIX)

πŸ’– STRLEN(3POSIX) β€” the POSIX Programmer's Manual, but make it cute πŸ’–

okay bestie, so this is a page from the POSIX Programmer's Manual (yes, the POSIX Programmer's Manual, she's an icon, she's a legend) and we're talking about strlen and strnlen today. πŸŽ€


🚨 PROLOG (read this first, fr fr)

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so like, consult the corresponding Linux manual page for details of Linux behavior β€” or honestly? the interface may not be implemented on Linux at all. πŸ’€ so don't come for me if your box says otherwise.


✨ NAME ✨

strlen, strnlen β€” get length of fixed size string πŸ‘‘


🌸 SYNOPSIS 🌸

#include <string.h>

size_t strlen(const char *s);
size_t strnlen(const char *s, size_t maxlen);

πŸ’… DESCRIPTION πŸ’…

So for strlen(): the functionality described on this reference page is aligned with the ISO C standard. If there's any conflict between the requirements described here and the ISO C standard, that conflict is unintentional β€” no drama intended, okay? This volume of POSIX.1‐2017 defers to the ISO C standard. periodt. πŸ’–

The strlen() function shall compute the number of bytes in the string to which s points, not including the terminating NUL character. she counts the vibe, not the void. ✨

The strnlen() function shall compute the smaller of:

  • the number of bytes in the array to which s points, not including any terminating NUL character, or
  • the value of the maxlen argument.

and the strnlen() function shall never examine more than maxlen bytes of the array pointed to by s. she stays in her lane, respectfully. 🫢


πŸ¦‹ RETURN VALUE πŸ¦‹

The strlen() function shall return the length of s β€” and no return value shall be reserved to indicate an error. so like, there's no special "oops" number hiding in there. it's just the length. that's the whole thing. 😌

The strnlen() function shall return the number of bytes preceding the first null byte in the array to which s points, if s contains a null byte within the first maxlen bytes; otherwise, it shall return maxlen.


πŸ™ˆ ERRORS πŸ™ˆ

No errors are defined.

and the following sections are informative. iykyk. πŸ’œ

🌈 EXAMPLES 🌈

πŸ“ Getting String Lengths

The following example sets the maximum length of key and data by using strlen() to get the lengths of those strings.

#include <string.h>
...
struct element {
    char *key;
    char *data;
};
...
char *key, *data;
int len;

*keylength = *datalength = 0;
...
if ((len = strlen(key)) > *keylength)
    *keylength = len;
if ((len = strlen(data)) > *datalength)
    *datalength = len;
...

look at her go. tracking the max like the main character she is. πŸ‘‘


🧚 APPLICATION USAGE 🧚

None.


⭐ RATIONALE ⭐

None.


πŸ”₯ FUTURE DIRECTIONS πŸ”₯

None.


πŸ’œ SEE ALSO πŸ’œ

  • wcslen()

The Base Definitions volume of POSIX.1‐2017, <string.h>


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.

In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. (she's the final boss, no appeals. πŸ’…) The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


okay that's the whole page bestie, we ate and left absolutely zero crumbs. πŸŽ€βœ¨

signed, sealed, delivered by IEEE/The Open Group, 2017, and yeah β€” STRLEN(3POSIX), that's her name, don't wear it out. πŸ¦‹πŸ’–