Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strlen function + include the terminating null character ?

Reply
Thread Tools

strlen function + include the terminating null character ?

 
 
lasek
Guest
Posts: n/a
 
      03-25-2005
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.

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      03-25-2005
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
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      03-25-2005
"lasek" <(E-Mail Removed)> 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
 
Reply With Quote
 
lasek
Guest
Posts: n/a
 
      03-25-2005
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......

 
Reply With Quote
 
dot@dot.dot
Guest
Posts: n/a
 
      03-25-2005
On Fri, 25 Mar 2005 09:07:12 -0500, "lasek" <(E-Mail Removed)>
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.


 
Reply With Quote
 
lasek
Guest
Posts: n/a
 
      03-25-2005
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...

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      03-25-2005
On Fri, 25 Mar 2005 09:07:12 -0500, in comp.lang.c , "lasek"
<(E-Mail Removed)> 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>
 
Reply With Quote
 
Rufus V. Smith
Guest
Posts: n/a
 
      05-20-2005

"Richard Bos" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "lasek" <(E-Mail Removed)> 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



 
Reply With Quote
 
Rufus V. Smith
Guest
Posts: n/a
 
      05-20-2005

"Richard Bos" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "lasek" <(E-Mail Removed)> 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



 
Reply With Quote
 
Rufus V. Smith
Guest
Posts: n/a
 
      05-20-2005

"Richard Bos" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "lasek" <(E-Mail Removed)> 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




 
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
What to Include to Use <strlen()> Seebs C Programming 3 09-16-2009 09:16 AM
NULL to strlen function sam_cit@yahoo.co.in C Programming 44 01-18-2007 06:10 AM
strlen() without include <cstring> howachen@gmail.com C++ 3 10-08-2006 01:00 PM
Problem with character string loop and strlen() No Such Luck C Programming 9 07-10-2005 12:25 PM
strlen + terminating null character lasek C Programming 5 03-26-2005 12:07 AM



Advertisments