简体   繁体   English

strlen - 字符串的长度有时会增加1

[英]strlen - the length of the string is sometimes increased by 1

I'm doing some C puzzle questions. 我正在做一些C拼图问题。 In most cases, I am able to find the right answer, but with that one I am having problems. 在大多数情况下,我能够找到正确的答案,但有了这个问题,我就遇到了问题。 I know the right answer by using the compiler, but I don't know the reason. 我通过使用编译器知道正确的答案,但我不知道原因。

Have a look at the code: 看看代码:

char c[] = "abc\012\0x34";

What would strlen(c) return, using a Standard C compiler? strlen(c)会使用标准C编译器返回什么?

My compiler returns 4 when what I expected was 3. 当我期望的是3时,我的编译器返回4。

What I thought is strlen() would search for the first occurrence of the NULL character but somehow the result is one more than I expected. 我认为是strlen()将搜索第一次出现的NULL字符,但不知何故结果是比我预期的多一个。

Any idea why? 知道为什么吗?

Let's write 让我们写

char c[] = "abc\012\0x34";

with single characters: 单个字符:

char c[] = { 'a', 'b', 'c', '\012', '\0', 'x', '3', '4', '\0' };

The first \\0 you see is the start of an octal escape sequence \\012 that extends over the following octal digits. 您看到的第一个\\0是八进制转义序列\\012的开头,它延伸到以下八进制数字。

Octal escape sequences are specified in section 6.4.4.4 of the standard (N1570 draft): 八进制转义序列在标准(N1570草案)的6.4.4.4节中规定:

octal-escape-sequence: 八进制转义序列:
\\ octal-digit \\ octal-digit
\\ octal-digit octal-digit \\ 八位数八进制数字
\\ octal-digit octal-digit octal-digit \\ octal-digit八进制数字八进制数字

they consist of a backslash followed by one, two, or three octal digits. 它们由反斜杠后跟一个,两个或三个八进制数字组成。 In paragraph 7 of that section, the extent of octal and hexadecimal escape sequences is given: 在该节第7段中,给出了八进制和十六进制转义序列的范围:

7 Each octal or hexadecimal escape sequence is the longest sequence of characters that can constitute the escape sequence. 7每个八进制或十六进制转义序列是可以构成转义序列的最长字符序列。

Note that while the length of an octal escape sequence is limited to at most three octal digits (thus "\\123456" consists of five characters, { '\\123', '4', '5', '6', '\\0' } ), hexadecimal escape sequences have unlimited length 请注意,虽然八进制转义序列的长度限制为最多三个八进制数字(因此"\\123456"由五个字符组成, { '\\123', '4', '5', '6', '\\0' } ),十六进制转义序列具有无限长度

hexadecimal-escape-sequence: 十六进制转义序列:
\\x hexadecimal-digit \\x 十六进制数字
hexadecimal-escape-sequence hexadecimal-digit 十六进制转义序列十六进制数字

and thus "\\x123456789abcdef" consists of only two characters ( { '\\x123456789abcdef', '\\0' } ). 因此"\\x123456789abcdef"只包含两个字符( { '\\x123456789abcdef', '\\0' } )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM