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.
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.
In order to use strlen() you need to include the <cstring> header file:
#include <cstring>
This was the answer I was looking for, but I didn't find a direct answer here.
You should be looking in glibc, not GCC -- it seems to be defined in strlen.c -- here's a link to strlen.c for glibc version 2.7... And here is a link to the glibc SVN repository online for strlen.c.
The reason you should be looking at glibc and not gcc is:
The GNU C library is used as the C library in the GNU system and most systems with the Linux kernel.
Here's the bsd implementation
size_t
strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s)
;
return (s - str);
}