Which library does strlen() belong to? Does it belong to cstring? or string?

Neither. cstring and string are not libraries, they are header files which define the interface to various functions and classes.

The C language standard says that the strlen function is declared in the header file <string.h>. In C++, including <string.h> places strlen into the global namespace, while including <cstring> instead places strlen into the std namespace.

The actual implementation of the strlen function is in the C standard library (aka libc or CRT on certain platforms). Ordinarily, this is linked in with your executable at link time.

Why it works without including library string or cstring?

In your particular compiler and toolchain, it just so happens that the header file <iostream> includes <cstring> into it, which means that any code that includes the former also gets the latter for free. This is an implementation detail and should not be relied upon—if your compile your code with another compiler, you may suddenly find yourself in a sea of compiler errors.

The proper thing to do is to also include <cstring> here; even though it's not necessary with your particular compiler, it may be necessary with other compilers.

Answer from Adam Rosenfield on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › c_standard_library › c_function_strlen.htm
C library function - strlen()
The C library strlen() function is used to calculates the length of the string. This function doesn't count the null character '\0'. In this function, we pass the pointer to the first character of the string whose length we want to determine ...
🌐
Programiz
programiz.com › c-programming › library-function › string.h › strlen
C strlen() - C Standard Library
It is defined in the <string.h> header file. #include <stdio.h> #include <string.h> int main() { char a[20]="Program"; char b[20]={'P','r','o','g','r','a','m','\0'}; // using the %zu format specifier to print size_t printf("Length of string a = %zu \n",strlen(a)); printf("Length of string b = %zu \n",strlen(b)); return 0; }
🌐
Wikibooks
en.wikibooks.org › wiki › C_Programming › string.h › strlen
C Programming/string.h/strlen - Wikibooks, open books for an open world
In the C standard library, strlen is a string function that determines the length of a C character string.
🌐
Scaler
scaler.com › home › topics › c strlen()
C strlen() - Scaler Topics
July 14, 2024 - strlen() is one such library function included under the string.h header file. We know that strings are one-dimensional arrays terminated by a NULL character (\0). The strlen() function in C takes the string as a parameter and returns the length of the string excluding the NULL character.
🌐
CS50
manual.cs50.io › 3 › strlen
strlen - CS50 Manual Pages
The strlen() function returns the number of bytes in the string pointed to by s. #include <cs50.h> #include <stdio.h> #include <string.h> int main(void) { string s = get_string("Input: "); printf("Output: "); for (int i = 0, n = strlen(s); i < n; i++) { printf("%c", s[i]); } printf("\n"); }
🌐
Cplusplus
cplusplus.com › reference › cstring › strlen
strlen
size_t strlen ( const char * str ); Get string length · Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
🌐
GeeksforGeeks
geeksforgeeks.org › c language › strlen-function-in-c
strlen() function in c - GeeksforGeeks
May 29, 2023 - The strlen() function in C calculates the length of a given string. The strlen() function is defined in string.h header file.
Find elsewhere
🌐
GNU
gnu.org › software › libc › manual › html_node › String-Length.html
String Length (The GNU C Library)
strlen (s) : maxlen) but it is ... strnlen (string, 5) ⇒ 5 · This function is part of POSIX.1-2008 and later editions, but was available in the GNU C Library and other systems as an extension long before it was standardized....
🌐
Code Browser
codebrowser.dev › glibc › glibc › string › strlen.c.html
strlen.c source code [glibc/string/strlen.c] - Codebrowser
Generated while processing glibc/benchtests/bench-strlen.c Generated on 2026-Feb-08 from project glibc revision glibc-2.39-288-gce65d944e3 Powered by Code Browser 2.1 Generator usage only permitted with license.
🌐
Educative
educative.io › answers › how-to-use-the-strlen-function-in-c
How to use the strlen() function in C
The following is the syntax of the strlen() function: ... Let's see some examples to use the strlen() method in C. The following example takes a simple string string1 and calculates its length. ... The strlen() function from the string.h library is ...
🌐
GitHub
github.com › lattera › glibc › blob › master › string › strlen.c
glibc/string/strlen.c at master · lattera/glibc
February 5, 2022 - GNU Libc - Extremely old repo used for research purposes years ago. Please do not rely on this repo. - glibc/string/strlen.c at master · lattera/glibc
Author   lattera
🌐
Vultr
docs.vultr.com › clang › standard-library › string-h › strlen
C string.h strlen() - Get String Length | Vultr Docs
September 27, 2024 - The strlen() function in C, provided by the string.h library, plays a crucial role in string handling by returning the length of a string. This length is calculated as the number of characters preceding the null terminator (\0), which is used ...
🌐
Fresh2Refresh
fresh2refresh.com › home › c programming tutorial › c – string › c – strlen() function
strlen() function in C | C String | Fresh2Refresh.com
September 22, 2020 - strlen() function in C:strlen( ) function in C gives the length of the given string. Syntax for strlen( ) function is given below.size_t strlen ( const char
🌐
W3Schools
w3schools.com › c › ref_string_strlen.php
C string strlen() Function
This is smaller than the amount of memory that is reserved for the string, which can be measured using the sizeof operator instead. The strlen() function is defined in the <string.h> header file.
🌐
Cppreference
en.cppreference.com › w › c › string › byte › strlen
strlen, strnlen_s - cppreference.com
June 13, 2024 - #define __STDC_WANT_LIB_EXT1__ 1 #include <stdio.h> #include <string.h> int main(void) { const char str[] = "How many characters does this string contain?"; printf("without null character: %zu\n", strlen(str)); printf("with null character: %zu\n", sizeof str); #ifdef __STDC_LIB_EXT1__ printf("without null character: %zu\n", strnlen_s(str, sizeof str)); #endif }
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › strlen-wcslen-mbslen-mbslen-l-mbstrlen-mbstrlen-l
strlen, wcslen, _mbslen, _mbslen_l, _mbstrlen, _mbstrlen_l | Microsoft Learn
December 2, 2022 - The output value is affected by the setting of the LC_CTYPE category setting of the locale. For more information, see setlocale. The versions of these functions without the _l suffix use the current locale for this locale-dependent behavior; the versions with the _l suffix are identical except that they use the locale parameter passed in instead. For more information, see Locale. For more compatibility information, see Compatibility. // crt_strlen.c // Determine the length of a string.
🌐
Sdds
intro2c.sdds.ca › string library
String Library | Introduction to C - Table of contents
These are four of the most common string functions, but there are many available in the string library. The header file that contains the prototypes for these library functions is: ... The strlen() function returns the number of characters in the character string excluding the null terminator byte.