As name literal itself implies string literal is a sequence of characters enclosed in double quotes. Implicitly this sequence of characters is appended by a terminating zero.
So any character enclosed in the double quotes is a part of the string literal.
When a string literal is used to initialize a character array all its characters including the terminating zero serve as initializers of the corresponding elements of the character array.
Each string literal in turn has type of a character array.
For example this string literal "Hello\0Hi"
in C has type char[9]
: 8 characters enclosed in the quotes plus the implicit terminating zero.
So in memory this string literal is stored like
{ 'H', 'e', 'l', 'l', 'o', '\0', 'H', 'i', '\0' }
Operator sizeof
returns the number of bytes occupied by an object. So for the string literal above the operator sizeof
will return value 9
- it is the number of bytes occupied by the literal in memory.
If you wrote "Hello\0Hi"
then the compiler may not itself just remove this part Hi
from the literal. It has to store it in memory along with other characters of the literal enclosed in quotes.
The sizeof
operator returns the size in bytes of any object in C not only of character arrays.
In general character arrays can store any raw data for example some binary data read from a binary file. In this case this data is not considered by the user and by the program like strings and as result are processed differently than strings.
Standard C function strlen
is specially written for character arrays that to find the length of a stored string in a character array. It does not know what data are stored in an array and how they were written in it. All what it does is searches the first zero character in a character array and returns the number of characters in the character array before the zero character.
You can store in one character array several strings sequentially. For example
char s[12];
strcpy( s, "Hello" );
strcpy( s + sizeof( "Hello" ), "World" );
puts( s ); // outputs "Hello"
puts( s + sizeof( "Hello" ) ); // outputs "World"
If you would define a two dimensional array like this
char t[2][6] = { "Hello", "World" };
then in memory it will be stored the same way as the one-dimensional array above. So you can write
char *s = ( char * )t;
puts( s ); // outputs "Hello"
puts( s + sizeof( "Hello" ) ); // outputs "World"
Another example. Standard C function strtok
can split one string stored in a character array to several strings substituting the specified by the user delimiters with zero bytes. As result the character array will contain several strings.
For example
char s[] = "Hello World";
printf( "%zu\n", sizeof( s ) ); // outputs 12
strtok( s, " " );
puts( s ); // outputs "Hello"
puts( s + sizeof( "Hello" ) ); // outputs "World"
printf( "%zu\n", sizeof( s ) ); // outputs 12
The last printf statement will output the same value equal to 12 because the array occupies the same number of bytes. Simply one byte in the memory allocated for the array was changed from ' '
to '\0'
.