strlen() without include <cstring>

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • howachen@gmail.com

    #1

    strlen() without include <cstring>

    from many books said, to use strlen(), you need to include <cstringor
    <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?

  • Daniel T.

    #2
    Re: strlen() without include &lt;cstring& gt;

    howachen@gmail. com wrote:
    from many books said, to use strlen(), you need to include <cstringor
    <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.

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longerÊhave anyone to discuss your doubts with,
    nor any ability to discuss them.

    Comment

    • Pete Becker

      #3
      Re: strlen() without include &lt;cstring& gt;

      howachen@gmail. com wrote:
      >
      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
      <iostreamprovid es 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.

      Comment

      • Jacek Dziedzic

        #4
        Re: strlen() without include &lt;cstring& gt;

        howachen@gmail. com wrote:
        from many books said, to use strlen(), you need to include <cstringor
        <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.

        Comment

        Working...