By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,559 Members | 1,180 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,559 IT Pros & Developers. It's quick & easy.

Please explain why ?

P: n/a
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'};

int len;
len=strlen(ch);

printf("%d\n",len);
}
////////////////////////////////////////////////
Nov 13 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
sa***********@yahoo.com (Sanjeev) wrote in
news:ac**************************@posting.google.c om:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'}; <--- NO terminating NULL!

int len;
len=strlen(ch);
ch[] is an array of two chars, not a C string.
printf("%d\n",len);
}
////////////////////////////////////////////////


Thus, strlen() will run until it encounters a NULL somewhere in memory
after the memory location of ch[1].

Fix: either treat ch[] like a string or don't use string functions on it.
E.g.

char ch[] = { 'a', 'b', '\0' };

or

char ch[SOME_SIZE];

strncpy(ch, "ab", sizeof ch);

--
- Mark ->
--
Nov 13 '05 #2

P: n/a
Sanjeev <sa***********@yahoo.com> scribbled the following:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3). Please explain why ? ////////////////////////////////////////////////
#include<stdio.h>
#include<string.h> void main()
Insert complaint about void main() here. I'm too tired.
{
char ch[]={'a','b'}; int len;
len=strlen(ch);
Bang. You're dead. ch is not a string. It's an array of char, but
not a null-terminated one. You've just invoked undefined behaviour.
printf("%d\n",len);
}
////////////////////////////////////////////////


The output might be anything your compiler decides, because you've let
strlen() wander all of to memory you don't even own.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
Nov 13 '05 #3

P: n/a
On 23 Jul 2003 13:50:13 -0700, sa***********@yahoo.com (Sanjeev) wrote:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).
The output is undefined and can be anything.
Please explain why ?
The string you pass to strlen is not zero-terminated.

If you want a zero-terminated string use
char ch[] = "ab";
or
char ch[] = { 'a', 'b', '\0' };

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'};

int len;
len=strlen(ch);

printf("%d\n",len);
}
////////////////////////////////////////////////


Nov 13 '05 #4

P: n/a
Sanjeev <sa***********@yahoo.com> wrote:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'};

int len;
len=strlen(ch);

printf("%d\n",len);
}
////////////////////////////////////////////////


oops. ch is not a string...just an array of two characters.

Most likely what happened is that once strlen went past the end of your
array, it was another 5 characters before a value of zero was found.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #5

P: n/a
On 23 Jul 2003 13:50:13 -0700, in comp.lang.c ,
sa***********@yahoo.com (Sanjeev) wrote:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).


You're a lucky boy. It could have printed 45667687798 or "memory
access violation, core dumped" or "hoo, its hedgehog season. lets
paint a moonrock loud".

Strlen counts chars till it finds a \0. Your array doesn't have room
for a \0, so strlen will keep going, wandering into memory that your
program doesn't own, until it finds such a character.

This might take forever, or your computer might not let you examine
memory you don't own, and might then warn you, or crash, or emit
nonsense.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #6

P: n/a
sa***********@yahoo.com (Sanjeev) wrote (23 Jul 2003) in
news:ac**************************@posting.google.c om / comp.lang.c:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?
Because it makes no sense to apply strlen to char ch[]={'a','b'}; which is not a string at all.

Nor does it make sense to ask why any program does what it does after
your illegal use of void main()


--
Martin Ambuhl
Returning soon to the
Fourth Largest City in America
Nov 13 '05 #7

P: n/a
In article <ac**************************@posting.google.com >,
sa***********@yahoo.com (Sanjeev) wrote:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main() ***********

Using "void main ()" instead of "int main ()" produces undefined
behavior and marks you as clueless. Avoid doing this. If you had a
teacher telling you to use void main () ask him to post on comp.lang.c
and we will rip his head off.
{
char ch[]={'a','b'};

int len;
len=strlen(ch);
strlen () expects a string. Find out what the format of a string is. ch
[] is _not_ a string. Once you know what the format of a string is, it
will be obvious why ch is not a string.
printf("%d\n",len);
}
////////////////////////////////////////////////

Nov 13 '05 #8

P: n/a

"Sanjeev" <sa***********@yahoo.com> wrote in message
news:ac**************************@posting.google.c om...
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?

////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>

void main()


Read the FAQ 11.12 to 11.15
http://www.eskimo.com/~scs/C-faq/top.html


Nov 13 '05 #9

P: n/a
In <ac**************************@posting.google.com > sa***********@yahoo.com (Sanjeev) writes:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?
Please explain why you expect 2 or 3.
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'};

int len;
len=strlen(ch);

printf("%d\n",len);
}


Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

P: n/a
Da*****@cern.ch (Dan Pop) wrote in message news:<bf*********@sunnews.cern.ch>...
In <ac**************************@posting.google.com > sa***********@yahoo.com (Sanjeev) writes:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).

Please explain why ?
Please explain why you expect 2 or 3.

I was expecting 2 or 3 because i thought ch[] is a string and if
compiler counts '\0' it should come up with 3 otherwise 2.

But now i got how it should have been done.

Thanks everbody for their support.
#include<stdio.h>
#include<string.h>

void main()
{
char ch[]={'a','b'};

int len;
len=strlen(ch);

printf("%d\n",len);
}


Dan

Nov 13 '05 #11

P: n/a
Sanjeev <sa***********@yahoo.com> scribbled the following:
Da*****@cern.ch (Dan Pop) wrote in message news:<bf*********@sunnews.cern.ch>...
In <ac**************************@posting.google.com > sa***********@yahoo.com (Sanjeev) writes:
>Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).
>
>Please explain why ?
Please explain why you expect 2 or 3. I was expecting 2 or 3 because i thought ch[] is a string and if
compiler counts '\0' it should come up with 3 otherwise 2.
Do you see a '\0' anywhere in the code you posted?
But now i got how it should have been done. Thanks everbody for their support.

>#include<stdio.h>
>#include<string.h>
>
>void main()
>{
> char ch[]={'a','b'};
>
> int len;
> len=strlen(ch);
>
> printf("%d\n",len);
>}


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"I am lying."
- Anon
Nov 13 '05 #12

P: n/a
Sanjeev wrote:
I was expecting 2 or 3 because i thought ch[] is a string and if
compiler counts '\0' it should come up with 3 otherwise 2.


A C style [character] string is an array of characters
terminated by a nul character '\0' which serves as a the *sentinel*.
The length of the string is the number of character up to
but *not* including the nul character.
An array of characters that begins with a nul character
is called the *empty* string.

The [character] string literal "Hello world!"
has length 12 and is stored in a character array
which is *at least* 13 characters long
because there *must* a nul character
immediately after the end of the string.

Nov 13 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.