strlen() without include <cstring>

H

howachen

from many books said, to use strlen(), you need to include <cstring> or
<string.h>, but i found the following program work, why?

#include <iostream>

using namespace std;

int main() {

char *c = "test";

cout<<strlen(c);

}


i know it is from <iostream>, so this mean the books are wrong now?
 
D

Daniel T.

from many books said, to use strlen(), you need to include <cstring> or
<string.h>, but i found the following program work, why?

#include <iostream>

using namespace std;

int main() {

char *c = "test";

cout<<strlen(c);

}


i know it is from <iostream>, so this mean the books are wrong now?

In your particular implementation, iostream includes cstring, that
doesn't need to be the case though and your code might fail in some
other compiler.
 
P

Pete Becker

i know it is from <iostream>, so this mean the books are wrong now?

No. It just means that with the standard library you're using, #include
<iostream> provides a prototype for strlen.

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
J

Jacek Dziedzic

from many books said, to use strlen(), you need to include <cstring> or
<string.h>, but i found the following program work, why?

#include <iostream>

using namespace std;

int main() {

char *c = "test";

cout<<strlen(c);

}


i know it is from <iostream>, so this mean the books are wrong now?

Consider an analogy. The books for driving licence candidates
tell you not to drive when you see a red light. Yet, sometimes
you may get lucky and manage to drive through a pedestrian
crossing at a red light and not hurt anyone.
Does that mean the books are wrong now?

HTH,
- J.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top