For example:
char a[] = "abc\0";
Does standard C say that another byte of value 0
must be appended even if the string already has a zero at the end? So, is sizeof(a)
equal to 4 or 5?
All string literals have an implicit null-terminator, irrespective of the content of the string. The standard (6.4. 5 String Literals) says: A byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.
Because in C strings are just a sequence of characters accessed viua a pointer to the first character. There is no space in a pointer to store the length so you need some indication of where the end of the string is. In C it was decided that this would be indicated by a null character.
Strings are actually one-dimensional array of characters terminated by a null character '\0'.
A null-terminated string means that the end of your string is defined through the occurrence of a null-char (all bits are zero).
All string literals have an implicit null-terminator, irrespective of the content of the string.
The standard (6.4.5 String Literals) says:
A byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals.
So, the string literal "abc\0"
contains the implicit null-terminator, in addition to the explicit one. So, the array a
contains 5 elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With