On this page
strlen
Prev: memchr | Up: string.h | Next: strcmp
Rel: string.h, stdlib.h & string.h Guide
Ref: cppreference — strlen
Tags: #public #prog #c #stdlib #string #tier/core
Include
#include <string.h>
Signature & Overview
size_t strlen(const char *str);
Returns the byte length of a NUL-terminated string in $O(n)$, excluding '\0'.
Notes & Pitfalls
- $O(n)$ scan up to
'\0': Never callstrlen(s)inside afor (size_t i = 0; i < strlen(s); ++i)loop condition (turns $O(n)$ into $O(n^2)$)—hoistsize_t len = strlen(s);out.
Example
#include <assert.h>
#include <string.h>
int main(void) {
const char *s = "hello";
size_t len = strlen(s);
assert(len == 5);
}
Linked from
Related notes
- tuts/c/stdlib/string/memchr Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcmp Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string Linked here; links to this page; shared tags: #c, #prog, #stdlib, #string
- tuts/c/stdlib/stdlib_and_string_h Linked here; shared tags: #c, #prog, #stdlib
- tuts/c/stdlib/string/memcmp shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memcpy shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memmove shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/memset shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strcat shared tags: #c, #prog, #stdlib, #string, #tier
- tuts/c/stdlib/string/strchr shared tags: #c, #prog, #stdlib, #string, #tier