Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

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

I'm doing some C puzzle questions. 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[] = "abc12x34";

What would strlen(c) return, using a Standard C compiler?

My compiler returns 4 when what I expected was 3.

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.

Any idea why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Let's write

char c[] = "abc12x34";

with single characters:

char c[] = { 'a', 'b', 'c', '12', '', 'x', '3', '4', '' };

The first you see is the start of an octal escape sequence 12 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', '' }), 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', '' }).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...