473,480 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

strlen function + include the terminating null character ?

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
8 7366
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
"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
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
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
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

"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

"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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
11593
by: Matt Parkins | last post by:
Hi, (I realise this probably isn't precisely the right group for this - could someone direct me to the appropriate group to post this question? - thanks !) I'm using Visual C++ 2005 Express...
12
2732
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
21
8445
by: sugaray | last post by:
hi, it just came up my mind that since we can get the length of any given string literal S with 'sizeof S-1', so, what's the merit of library function strlen()'s existence ? thanx in advance for...
81
7216
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
66
7691
by: roy | last post by:
Hi, I was wondering how strlen is implemented. What if the input string doesn't have a null terminator, namely the '\0'? Thanks a lot Roy
83
15506
by: rahul8143 | last post by:
hello, what is difference between sizeof("abcd") and strlen("abcd")? why both functions gives different output when applied to same string "abcd". I tried following example for that. #include...
13
1512
by: Ivar | last post by:
Hi guys - So basically I am trying to implement a function that converts an int to a string, but it is not working for some reason - any thoughts? My function, intToStr, is shown below. I'm just...
6
7591
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
44
24954
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.