20

Which library does strlen() belong to?

Does it belong to cstring? or string?

I tried the following code, and it does work:

include <iostream>
using namespace std;
//withou include<string.h>

int main() {
    char * str="abc";
    cout<<strlen(str);
}

I set str equal to 3 and give the right answer 3.

Why does it work without including library string or cstring?

Should I include cstring or string there? string.h?

pb2q
  • 58,613
  • 19
  • 146
  • 147
user2756494
  • 263
  • 1
  • 4
  • 7
  • 4
    This question appears to be off-topic because it is already answered by the official documentation. – John Dvorak Oct 01 '13 at 04:13
  • 2
    @ImranOmer this doesn't look like PHP. Rather, it looks like C++. – John Dvorak Oct 01 '13 at 04:14
  • 2
    @JanDvorak: If it is answered in the official documentation, that seems to make it an on-topic question. Why would the docs go off-topic? – Thilo Oct 01 '13 at 04:15
  • @Dvorak: I was confused since he doesn't used C++ as keyword. I thought he is using PHP. nevermind – Imran Omer Oct 01 '13 at 04:17
  • @Thilo "offtopic" means "doesn't belong here and should be closed" rather "isn't about programming". I expect the closing system to change again in the future, hopefully clarifying this. – John Dvorak Oct 01 '13 at 04:17
  • 1
    If "answered by documentation" is not a valid reason to close a question, then I'm switching to "lacks minimal understanding" as the official reason. – John Dvorak Oct 01 '13 at 04:20
  • Voting to reopen. The underlying reason is a non-trivial difference between C and C++ (indirect inclusion of headers is only allowed in C++), yet the function is originally from C. – MSalters Oct 01 '13 at 08:09
  • 3
    @JanDvorak many "good" questions on SO (measured by upvotes) can be answered with a cursory reading of the relevant spec; why single this one out? – SheetJS Oct 08 '13 at 03:11

5 Answers5

20

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.

Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
12

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.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
MBI
  • 583
  • 8
  • 22
8

The function strlen() is declared in the header file <string.h>.

From the GNU C Library.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
slash28cu
  • 1,614
  • 11
  • 23
-1
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char *pointer1="Idle hands are the devil's workshop.";

    int length;
    length=strlen(pointer1);

    char *pointer2;
    pointer2=new char(length+1);

    strcpy(pointer2,pointer1);

    cout<<"pointer 2:"<<pointer2;
    delete[] pointer2;

    return 0;
}
Ammar
  • 1
-4
#include <stdio.h>
#include <string.h>

int main(void)
{
    char *string = "Hello World";
    printf("%lu\n", (unsigned long)strlen(string));
    return 0;
}

This program will print the value 11, which is the length of the string "Hello World". Character strings are stored in an array of a data type called char. The end of a string is found by searching for the first null character in the array.

From the GNU C Library

John Dvorak
  • 26,799
  • 13
  • 69
  • 83