Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Please explain why ?

Reply
Thread Tools

Please explain why ?

 
 
Sanjeev
Guest
Posts: n/a
 
      07-23-2003
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);
}
////////////////////////////////////////////////
 
Reply With Quote
 
 
 
 
Allan Bruce
Guest
Posts: n/a
 
      07-23-2003

"Sanjeev" <> wrote in message
news: 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'};
>
> int len;
> len=strlen(ch);
>
> printf("%d\n",len);
> }
> ////////////////////////////////////////////////


This is actually undefined, since ch is an array of chars but is not null
terminated, if you had
char ch[] = "ab";
or
char ch[] = {'a','b','\0'};
then this would be fine and you would get your expected result
Allan


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-23-2003
"Allan Bruce" <> wrote...
>
> "Sanjeev" <> wrote in message
> news: 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'};
> >
> > int len;
> > len=strlen(ch);
> >
> > printf("%d\n",len);
> > }
> > ////////////////////////////////////////////////

>
> This is actually undefined, since ch is an array of chars but is not null
> terminated, if you had
> char ch[] = "ab";
> or
> char ch[] = {'a','b','\0'};
> then this would be fine and you would get your expected result
> Allan


Absolutely! Another reason why it's undefined is because the 'main'
function does not return 'int' (as it's supposed to).

Victor


 
Reply With Quote
 
Aggro
Guest
Posts: n/a
 
      07-23-2003
Sanjeev wrote:

> Output of followin program at Turbo C++ 3.0 is 7 ( Not 2 or 3).


You have posted this guestion to C++ and C groups. Please deside what
language you are learning. Is it C or is it C++? ( They are not the same
language )

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain this "Why's" example please Kaye Ng Ruby 8 06-08-2010 09:13 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
please explain why this is Undefined Behavior REH C++ 25 03-29-2005 10:19 PM
Please explain why ? Sanjeev C Programming 12 07-24-2003 11:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57