STRLEN(3P)

πŸ’– STRLEN(3P) β€” POSIX Programmer's Manual πŸ’–

okay so bestie, this is strlen and strnlen, and they're literally just here to tell you how long your strings are. no drama, no cap. but FIRST, a lil heads up πŸ‘‡

🌸 PROLOG 🌸

so like, this manual page is part of the POSIX Programmer's Manual. meaning: the Linux implementation of this interface may differ (so pls go check the corresponding Linux manual page for details on Linux behavior), or honestly the interface might not even be implemented on Linux at all. πŸ™ˆ just a vibe check before we go any further.


✨ 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 πŸ¦‹

okay so for strlen(): the functionality described on this reference page is aligned with the ISO C standard. if there's any conflict between what's written here and the ISO C standard, that conflict is unintentional β€” no bad blood. 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. that's it. that's the whole job. iconic behavior honestly πŸ’…

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 knows her limits, and she respects them. 🧚


πŸ’œ RETURN VALUE πŸ’œ

strlen() shall return the length of s. and listen β€” no return value shall be reserved to indicate an error. so if something's wrong, she's not telling you. she simply does not do error reports. 😭

strnlen() 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. simple as that fr fr.


πŸ“ ERRORS πŸ“

No errors are defined. literally none. she's unbothered. unproblematic. ✨

also the following sections are informative (iykyk, they're the bonus lore):


🌈 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. look at her go:

#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;
...

⭐ APPLICATION USAGE ⭐

None. nothing to add, bestie. we said what we said. πŸ’…


🫢 RATIONALE 🫢

None. no reasoning needed when you're simply correct. πŸ‘‘


🧚 FUTURE DIRECTIONS 🧚

None. the future is bright and this function is not changing. slay.


πŸ”₯ 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. so like, if they disagree, the original wins, no arguments. 🫑 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 .


aaaand that's the whole page bestie!! brought to you by IEEE/The Open Group, dated 2017, living under the name STRLEN(3P). go measure some strings, stay safe, and remember: strnlen will literally never look past maxlen bytes. obsessed with her boundaries fr. πŸ’–βœ¨πŸŽ€
πŸ“¦ where this page comes from