473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?

Oct 7 '06 #1
3 3830
ho******@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.
Oct 7 '06 #2
ho******@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
<iostreamprovides 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.
Oct 7 '06 #3
ho******@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.
Oct 8 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: wenmang | last post by:
Hi, all: What will happen if header files with the exact same names but with different contents and declared/defined without include guard reside in the include path for the compiler? Are these...
8
by: lasek | last post by:
Hi all, a simple question, look at this code below: char acName="Claudio"; unsigned int uiLen; uiLen=strlen(acName); printf("Length of acName variable %u",uiLen); //uiLen >>>> 7
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
7
by: Steven T. Hatton | last post by:
I'll admit, so far in my C++ experience, I've left ADL to the realm of 'magic happens'. I really don't know when I might be using it. Can someone provide examples of how ADL changes the behavior...
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
21
by: Anton Dec | last post by:
Just curious about this... malloc is defined in stdlib.h, right? But if I write a program without #include<stdlib.hand use malloc, it still works as expected. Why is this? Is malloc...
10
by: SoulIntruder | last post by:
Hello folks. I have a beginners question. I have such script: <?php $User = $_POST; $Password = $_POST; $Database = $_POST;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.