Consider this code:
char name[]=\"123\";
char name1[]=\"1234\";
And this result
The size of name (char[]):4
The size of n
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.
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.
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.
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.
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).
Every string is terminated with the char nullbyte '\0' which add 1 to your length.