strlen function + include the terminating null character ?

Discussion in 'C Programming' started by lasek, Mar 25, 2005.

  1. lasek

    lasek Guest

    Hi all, a simple question, look at this code below:

    char acName[]="Claudio";
    unsigned int uiLen;

    uiLen=strlen(acName);

    printf("Length of acName variable %u",uiLen);

    //uiLen >>>> 7

    Since strlen function does not include the terminating null character if i
    display:

    printf("acName[iLen]: [%c]",acName[iLen]);

    i should see something like [o].
    Why i see only [] ?.

    Thanks all.
     
    lasek, Mar 25, 2005
    #1
    1. Advertising

  2. lasek wrote:
    > Hi all, a simple question, look at this code below:
    >
    > char acName[]="Claudio";
    > unsigned int uiLen;
    >
    > uiLen=strlen(acName);
    >
    > printf("Length of acName variable %u",uiLen);
    >
    > //uiLen >>>> 7
    >
    > Since strlen function does not include the terminating null character if i
    > display:
    >
    > printf("acName[iLen]: [%c]",acName[iLen]);
    >
    > i should see something like [o].
    > Why i see only [] ?.
    >
    > Thanks all.
    >


    If you want to see a character, say %c. If you want to see the value of
    the character (0 in this case) use %d or some other int specifier. In
    the case of the nul character, nothing is being displayed.

    -David
     
    David Resnick, Mar 25, 2005
    #2
    1. Advertising

  3. lasek

    Richard Bos Guest

    "lasek" <> wrote:

    > char acName[]="Claudio";
    > unsigned int uiLen;
    >
    > uiLen=strlen(acName);
    >
    > printf("Length of acName variable %u",uiLen);


    > printf("acName[iLen]: [%c]",acName[iLen]);
    >
    > i should see something like [o].
    > Why i see only [] ?.


    Because arrays in C are zero-based. This should be the second thing your
    C text book teaches you about arrays, just after the difference between
    arrays and pointers.

    Richard
     
    Richard Bos, Mar 25, 2005
    #3
  4. lasek

    lasek Guest

    Ok ok...now i know, if i want to see the last char of the name "Claudio" i
    should write acName[6] because starting from zero, 0-6 is the last.

    I'm confuse because if i want store a string of 10 char i must declare an
    array of 10 elements and i write appo[10], but it's wrong, i need 11
    elements for the '\0' and so i write appo[11].
    But if i count from 0 to 11 i have 12 elements..
    Big trouble for my mind....

    Please help......
     
    lasek, Mar 25, 2005
    #4
  5. lasek

    Guest

    On Fri, 25 Mar 2005 09:07:12 -0500, "lasek" <>
    wrote:

    >Ok ok...now i know, if i want to see the last char of the name "Claudio" i
    >should write acName[6] because starting from zero, 0-6 is the last.
    >
    >I'm confuse because if i want store a string of 10 char i must declare an
    >array of 10 elements and i write appo[10], but it's wrong, i need 11
    >elements for the '\0' and so i write appo[11].
    >But if i count from 0 to 11 i have 12 elements..
    >Big trouble for my mind....
    >
    >Please help......


    The "number of elements" is not the same as the "numbering of the elements".

    In C arrays are always 0 based, 0 being the first element. Thus an array of
    10 elements is numbered 0 to 9.

    In other languages arrays can begin at arbitrary numbers such as in pascal
    where "Array[11..21]" is valid for a 10 element array. In that case the
    difference between number of elements (10) and the numbering of the elements
    (11,12,13,14...) is pretty obvious.

    "Claudio", for example, needs an array of 8 elements (7 letters plus the
    \0). In C those 8 elements are numbered 0, 1, 2, 3, 4, 5, 6 and 7.
     
    , Mar 25, 2005
    #5
  6. lasek

    lasek Guest

    After five minuts watching my two hands count number....WOW you are right.
    Thanks a lot for your patience and i know it was a stupid post but
    sometimes i lose my mind.
    Have a nice day...
     
    lasek, Mar 25, 2005
    #6
  7. On Fri, 25 Mar 2005 09:07:12 -0500, in comp.lang.c , "lasek"
    <> wrote:

    >I'm confuse because if i want store a string of 10 char i must declare an
    >array of 10 elements


    if you want to store a string of ten chars, you need ELEVEN elements,
    cos you need the 11th for the null.

    >But if i count from 0 to 11 i have 12 elements..


    you count from 0 to 10.

    char arr[10];

    has elements
    arr[0]
    through to
    arr[9]



    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 25, 2005
    #7
  8. "Richard Bos" <> wrote in message
    news:...
    > "lasek" <> wrote:
    >
    >> char acName[]="Claudio";
    >> unsigned int uiLen;
    >>
    >> uiLen=strlen(acName);
    >>
    >> printf("Length of acName variable %u",uiLen);

    >
    >> printf("acName[iLen]: [%c]",acName[iLen]);
    >>
    >> i should see something like [o].
    >> Why i see only [] ?.

    >
    > Because arrays in C are zero-based. This should be the second thing your
    > C text book teaches you about arrays, just after the difference between
    > arrays and pointers.
    >
    > Richard


    IOW, you are pointing past your string to the terminating null character.
    Null characters don't often have a display representation. The last
    character 'o' is at acName[uiLen - 1]

    Also, you should take care in some of the terminology.

    The length of acName variable is really sizeof(acName). This is the number
    of bytes (chars) of storage used. To be pedantic, if your char type is not
    byte-sized, you could use (sizeof(acName) / sizeof(acName[0])) to get the
    number of elements available in the array.

    The length of the string CONTAINEDIN the variable is strlen(acName)

    a string only goes up to the first null character, so it can be smaller than
    the array.

    Rufus
     
    Rufus V. Smith, May 20, 2005
    #8
  9. "Richard Bos" <> wrote in message
    news:...
    > "lasek" <> wrote:
    >
    >> char acName[]="Claudio";
    >> unsigned int uiLen;
    >>
    >> uiLen=strlen(acName);
    >>
    >> printf("Length of acName variable %u",uiLen);

    >
    >> printf("acName[iLen]: [%c]",acName[iLen]);
    >>
    >> i should see something like [o].
    >> Why i see only [] ?.

    >
    > Because arrays in C are zero-based. This should be the second thing your
    > C text book teaches you about arrays, just after the difference between
    > arrays and pointers.
    >
    > Richard


    IOW, you are pointing past your string to the terminating null character.
    Null characters don't often have a display representation. The last
    character 'o' is at acName[uiLen - 1]

    Also, you should take care in some of the terminology.

    The length of acName variable is really sizeof(acName). This is the number
    of bytes (chars) of storage used. To be pedantic, if your char type is not
    byte-sized, you could use (sizeof(acName) / sizeof(acName[0])) to get the
    number of elements available in the array.

    The length of the string CONTAINEDIN the variable is strlen(acName)

    a string only goes up to the first null character, so it can be smaller than
    the array.

    Rufus
     
    Rufus V. Smith, May 20, 2005
    #9
  10. "Richard Bos" <> wrote in message
    news:...
    > "lasek" <> wrote:
    >
    >> char acName[]="Claudio";
    >> unsigned int uiLen;
    >>
    >> uiLen=strlen(acName);
    >>
    >> printf("Length of acName variable %u",uiLen);

    >
    >> printf("acName[iLen]: [%c]",acName[iLen]);
    >>
    >> i should see something like [o].
    >> Why i see only [] ?.

    >
    > Because arrays in C are zero-based. This should be the second thing your
    > C text book teaches you about arrays, just after the difference between
    > arrays and pointers.
    >
    > Richard


    IOW, you are pointing past your string to the terminating null character.
    Null characters don't often have a display representation. The last
    character 'o' is at acName[uiLen - 1]

    Also, you should take care in some of the terminology.

    The length of acName variable is really sizeof(acName). This is the number
    of bytes (chars) of storage used. To be pedantic, if your char type is not
    byte-sized, you could use (sizeof(acName) / sizeof(acName[0])) to get the
    number of elements available in the array.

    The length of the string CONTAINEDIN the variable is strlen(acName)

    a string only goes up to the first null character, so it can be smaller than
    the array.

    Rufus
     
    Rufus V. Smith, May 20, 2005
    #10
    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. lasek

    strlen + terminating null character

    lasek, Mar 25, 2005, in forum: C Programming
    Replies:
    5
    Views:
    347
    Keith Thompson
    Mar 26, 2005
  2. No Such Luck

    Problem with character string loop and strlen()

    No Such Luck, Jul 7, 2005, in forum: C Programming
    Replies:
    9
    Views:
    398
    Joe Wright
    Jul 10, 2005
  3. NULL to strlen function

    , Jan 11, 2007, in forum: C Programming
    Replies:
    44
    Views:
    1,451
    Charles Richmond
    Jan 18, 2007
  4. Replies:
    3
    Views:
    428
    Jacek Dziedzic
    Oct 8, 2006
  5. Seebs

    What to Include to Use <strlen()>

    Seebs, Sep 15, 2009, in forum: C Programming
    Replies:
    3
    Views:
    2,495
    Nick Keighley
    Sep 16, 2009
Loading...

Share This Page