Please explain why ?

Discussion in 'C++' started by Sanjeev, Jul 23, 2003.

  1. Sanjeev

    Sanjeev Guest

    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);
    }
    ////////////////////////////////////////////////
    Sanjeev, Jul 23, 2003
    #1
    1. Advertising

  2. Sanjeev

    Allan Bruce Guest

    "Sanjeev" <> wrote in message
    news:...
    > 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
    Allan Bruce, Jul 23, 2003
    #2
    1. Advertising

  3. "Allan Bruce" <> wrote...
    >
    > "Sanjeev" <> wrote in message
    > news:...
    > > 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
    Victor Bazarov, Jul 23, 2003
    #3
  4. Sanjeev

    Aggro Guest

    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 )
    Aggro, Jul 23, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. REH
    Replies:
    25
    Views:
    821
    Victor Bazarov
    Mar 29, 2005
  2. Sanjeev

    Please explain why ?

    Sanjeev, Jul 23, 2003, in forum: C Programming
    Replies:
    12
    Views:
    527
    E. Robert Tisdale
    Jul 25, 2003
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  5. Kaye Ng
    Replies:
    8
    Views:
    167
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page