Please explain why ?

S

Sanjeev

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);
}
////////////////////////////////////////////////
 
J

Joona I Palaste

Sanjeev said:
Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).
Please explain why ?

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 ([email protected]) ---------------------------\
| 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
 
M

Mark A. Odell

(e-mail address removed) (Sanjeev) wrote in

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);
 
A

Alf P. Steinbach

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);
}
////////////////////////////////////////////////
 
E

Eric

Sanjeev said:
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.
 
M

Mark McIntyre

On 23 Jul 2003 13:50:13 -0700, in comp.lang.c ,
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.
 
M

Martin Ambuhl

(e-mail address removed) (Sanjeev) wrote (23 Jul 2003) in
/ 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
 
C

Christian Bau

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.
 
D

Dan Pop

In said:
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
 
S

Sanjeev

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
 
J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
E

E. Robert Tisdale

Sanjeev said:
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.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top