By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

strlen function + include the terminating null character ?

P: n/a
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.

Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Nov 14 '05 #2

P: n/a
"lasek" <cl**************@acrm.it> 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
Nov 14 '05 #3

P: n/a
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......

Nov 14 '05 #4

P: n/a
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...

Nov 14 '05 #5

P: n/a
On Fri, 25 Mar 2005 09:07:12 -0500, in comp.lang.c , "lasek"
<cl**************@acrm.it> 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>
Nov 14 '05 #6

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:42***************@news.individual.net...
"lasek" <cl**************@acrm.it> 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

Nov 14 '05 #7

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:42***************@news.individual.net...
"lasek" <cl**************@acrm.it> 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

Nov 14 '05 #8

P: n/a

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:42***************@news.individual.net...
"lasek" <cl**************@acrm.it> 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


Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.