473,226 Members | 1,718 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,226 software developers and data experts.

Please explain why ?

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
12 3030
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
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
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
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
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
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
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

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: DD | last post by:
Hello, Could anyone please help me?? Is there somebody who could explain me how to make a connection to a access database with a python cgi script. I would like to use common sql commands in my...
3
by: VijayShankar | last post by:
Can u be more specific on your question Anyway its not like Session variables are available for sometime and not available for sometime. When your session starts it is very much available...
1
by: Yash | last post by:
Hi, Can someone please explain to me what the StreamReader.DiscardBufferedData method does? The documentation says "Use DiscardBufferedData to seek to a known location in the underlying stream...
5
by: KathyB | last post by:
If someone could just explain this to me...I just don't get it! I have an aspx page where I retrieve several session variables and use xmlDocument to transform xml file with xsl file into an...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
9
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
3
by: sathishc58 | last post by:
Hi All, Here is the code which generates Segmentation Fault. Can anyone explain why the third printf fails and the first printf works? main() { char ch={"Hello"}; char *p; ...
2
by: sathishc58 | last post by:
Hi All Please explain why strlen returns() "16" as output here and explain the o/p for sizeof() as well main() { char a={'a','b','c'}; printf("strlen=%d\n", strlen(a));...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.