Reputation:
Consider this code:
char name[]="123";
char name1[]="1234";
And this result
The size of name (char[]):4
The size of name1 (char[]):5
Why the size of char[]
is always plus one?
Upvotes: 15
Views: 74468
Reputation: 4409
In C, strings are stored as arrays of char
s. With a recognised terminating character ('\0'
or just 0
) you can pass a pointer to the string, with no need for any further meta-data. When processing a string, you read chars from the memory pointed at by the pointer until you hit the terminating value.
As your array initialisation is using a string literal:
char name[]="123";
is equivalent to:
char name[]={'1','2','3',0};
If you want your array to be of size 3 (without the terminating character as you are not storing a string, you will want to use:
char name[]={'1','2','3'};
or
char name[3]="123";
(thanks alk) which will do as you were expecting.
Upvotes: 8
Reputation: 4823
As Michael pointed out in the comments the strings are terminated by a zero. So in memory the first string will look like this
"123\0"
where \0
is a single char and has the ASCII value 0. Then the above string has size 4.
If you had not this terminating character, how would one know, where the string (or char[]
for that matter) ends? Well, indeed one other way is to store the length somewhere. Some languages do that. C doesn't.
Upvotes: 18
Reputation: 1791
A String
in C (and in, probably, every programming language - behind the scenes) is an array of characters which is terminated by \0
with the ASCII value of 0.
When assigning: char arr[] = "1234";
, you assign a string literal, which is, by default, null-terminated (\0
is also called null) as you can see here.
To avoid a null (assuming you want just an array of char
s and not a string), you can declare it the following way char arr[] = {'1', '2', '3', '4'};
and the program will behave as you wish (sizeof(arr)
would be 4).
Upvotes: 2
Reputation: 34583
Note the difference between sizeof
and strlen
. The first is an operator that gives the size of the whole data item. The second is a function that returns the length of the string, which will be less than its sizeof
(unless you've managed to get string overflow), depending how much of its allocated space is actually used.
In your example
char name[]="123";
sizeof(name)
is 4, because of the terminating '\0'
, and strlen(name)
is 3.
But in this example:
char str[20] = "abc";
sizeof(str)
is 20, and strlen(str)
is 3.
Upvotes: 30
Reputation: 3735
In C, string literals have a null terminating character added to them.
Your strings,
char name[]="123";
char name1[]="1234";
look more like:
char name[]="123\0";
char name1[]="1234\0";
Hence, the size is always plus one. Keep in mind when reading strings from files or from whatever source, the variable where you store your string, should always have extra space for the null terminating character.
For example if you are expected to read string, whose maximum size is 100, your buffer variable, should have size of 101.
Upvotes: 1
Reputation: 19874
name = {'1','2','3','\0'};
name1 = {'1','2','3','4','\0'};
So
sizeof(name) = 4;
sizeof(name1) = 5;
sizeof
returns the size of the object and in this case the object is an array and it is defined that your array is 4 bytes long in first case and 5 bytes in second case.
Upvotes: 1
Reputation: 2825
Because there is a null character that is attached to the end of string in C.
Like here in your case
name[0] = '1'
name[1] = '2'
name[2] = '3'
name[3] = '\0'
name1[0] = '1'
name1[1] = '2'
name1[2] = '3'
name1[3] = '4'
name1[4] = '\0'
Upvotes: 3
Reputation: 1
Every string is terminated with the char nullbyte '\0' which add 1 to your length.
Upvotes: 0