473,714 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ 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(ac Name);

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 7393
lasek wrote:
Hi all, a simple question, look at this code below:

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

uiLen=strlen(ac Name);

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(ac Name);

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.in dividual.net...
"lasek" <cl************ **@acrm.it> wrote:
char acName[]="Claudio";
unsigned int uiLen;

uiLen=strlen(ac Name);

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.in dividual.net...
"lasek" <cl************ **@acrm.it> wrote:
char acName[]="Claudio";
unsigned int uiLen;

uiLen=strlen(ac Name);

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.in dividual.net...
"lasek" <cl************ **@acrm.it> wrote:
char acName[]="Claudio";
unsigned int uiLen;

uiLen=strlen(ac Name);

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
11707
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 Edition Beta (free download from MS - hooray!), and everything works fine, except I get warnings back on the use of some functions, strlen() for example, saying that the function has been deprecated - although they do still work (which is I guess...
12
2749
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 trying to get this to work, but been unable to solve my mistake. What the function should return is a file name used for creating logs files. It will look something like JN122345.log
21
8476
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 your instruction.
81
7307
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
66
7763
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
15584
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 <stdio.h> #include <string.h> void main() { char *str1="abcd";
13
1529
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 trying to implement this to gain practice with c-style strings. #include<iostream> #include"testString.h" int main(int argc, char* argv) { char* c2 = new char;
6
7609
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, Karthi
44
25067
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
8796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9307
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9071
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7946
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6627
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2514
muto222
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.