Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
例如:
char a[] = "abc\0";
标准 C 是否说0即使字符串末尾已经有一个零,也必须附加另一个字节的值?那么,sizeof(a)等于 4 还是 5?
0
sizeof(a)
无论字符串的内容如何,所有字符串文字都有一个隐式的空终止符。
标准(6.4.5 String Literals)说:
一个字节或零值代码被附加到由一个或多个字符串文字产生的每个多字节字符序列。
因此,字符串文字"abc\0"除了显式的外,还包含隐式的空终止符。因此,该数组a包含 5 个元素。
"abc\0"
a