以空终止符结尾的字符串文字是否包含额外的空终止符? [英] Do string literals that end with a null-terminator contain an extra null-terminator?
问题描述
例如:
char a[] = "abc\0";
标准 C 是否说即使字符串末尾已经有一个零,也必须附加另一个字节的值 0
?那么,sizeof(a)
等于 4 还是 5?
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.
标准(6.4.5 字符串文字)说:
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.
因此,字符串文字 "abc\0"
除了显式的空终止符之外,还包含隐式的空终止符.因此,数组 a
包含 5 个元素.
So, the string literal "abc\0"
contains the implicit null-terminator, in addition to the explicit one. So, the array a
contains 5 elements.
这篇关于以空终止符结尾的字符串文字是否包含额外的空终止符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!