I'm learning about arrays in C. What's the difference between the two statements when using character arrays?
char my_char_array[] = {'M','y',' ','S','t','r','i','n','g'};
and
char my_char_array[] = "My String";
Also, why do I get some unprintable characters if I try and print the value of my_char_array to the screen (this happens with my_char_array being initialized both ways)?
printf("Value of my_char_array[]:%s\n", my_char_array);
Value of my_char_array[]:My String╠╠╠╠╠╠╠╠╠╠╠
Main difference b/w these two arrays is the string terminating character \0
. The array,
char my_char_array[] = {'M','y',' ','S','t','r','i','n','g'};
does not have a string terminating character at the end. Therefore, printf
will continue printing until it reaches a \0
. However, C compiler, automatically inserts a string terminating character for the following array:
char my_char_array[] = "My String";
In short;
char my_char_array1[] = {'M','y',' ','S','t','r','i','n','g'};
char my_char_array2[] = "My String";
// are not equivalent
but,
char my_char_array1[] = {'M','y',' ','S','t','r','i','n','g', '\0'};
char my_char_array2[] = "My String";
// are equivalent
char v1[] = {'M','y',' ','S','t','r','i','n','g'};
char v2[] = "My String";
v2 is one byte longer than v1, and that last byte contains the null character which many apis (including printf's %s) use to determine the end of a c-string.
Like other answers mentioned that \0
is the main difference .
char my_char_array[] = {'M','y',' ','S','t','r','i','n','g'};
This statement defines a char
array. Whereas this statement
char my_char_array[] = "My String";
defines a string
which is, in C, also a char
array but with an extra \0
character at the end.
ie
char my_char_array[] = {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g', '\0'};
char my_char_array[] = "My String";
are equivalent but
char my_char_array[] = {'M', 'y', ' ', 'S', 't', 'r', 'i', 'n', 'g'};
char my_char_array[] = "My String";
are not. As for your output, you can't print a character array using %s
format specifier. You need to access it element-wise.
You may need to check these links first
I searched the C11 document (You can get the doc above link) and it says
6.7.9 Initialization
... skipped
14
An array of character type may be initialized by a character string
literal or UTF −8 string literal, optionally enclosed in braces.
Successive bytes of the string literal (including the terminating null
character if there is room or if the array is of unknown size) initialize
the elements of the array.
22
If an array of unknown size is initialized, its size is determined by
the largest indexed element with an explicit initializer. The array type
is completed at the end of its initializer list.
char my_char_array1[] = {'M','y',' ','S','t','r','i','n','g'};
char my_char_array2[] = "My String";
my_char_array1
variable is unknown size. so, the total size is to last 'g' initializer. There is no explicitly null-terminated.
my_char_array2
variables is also unknown size, but, successive bytes includes the null byte at the end of array.
printf
the function treats the null-terminated char array. It may print the characters until meeting the NULL byte. That's why you got strange characters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.