strlen - the length of the string is sometimes increased by 1

狂风中的少年 提交于 2019-11-27 14:57:05

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.

Octal escape sequences are specified in section 6.4.4.4 of the standard (N1570 draft):

octal-escape-sequence:
\ 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 Each octal or hexadecimal escape sequence is the longest sequence of characters that can constitute the escape sequence.

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

hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit

and thus "\x123456789abcdef" consists of only two characters ({ '\x123456789abcdef', '\0' }).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!