473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3039
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.