strlen function + include the terminating null character ?

L

lasek

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.
 
D

David Resnick

lasek said:
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
 
R

Richard Bos

lasek said:
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
 
L

lasek

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......
 
D

dot

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.
 
L

lasek

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...
 
M

Mark McIntyre

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]
 
R

Rufus V. Smith

Richard Bos said:
lasek said:
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
 
R

Rufus V. Smith

Richard Bos said:
lasek said:
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
 
R

Rufus V. Smith

Richard Bos said:
lasek said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top