Tip: (1) when calculating the length of a string, the key is to recognize the escape character. (2) an escape character always starts with a backslash and is composed of another character. Therefore, pay special attention to the backslash !!!
1. A string without escape characters
For example, "ABC! X =/", whose length is 7
2. character strings with escape characters
(1) string "ABC \ n": '\ n' is an escape character (line break). Only one character can be used to calculate the length of a string, therefore, the length of the string is 4 instead of 5)
(2) string "ABC \ n \ '\" ": contains four escape characters:' \ n' (line break) and '\' (backslash), '\ ''(single quotes),' \" '(double quotes), so the length of this string is 7 (rather than 11 ).
(3) string "ABC \ 0xyz": There is an escape character '\ 0', which is the string Terminator. Therefore, when the strlen function is used to test the length of the string, the result should be 3 (instead of 7 ).
(4) string "ABC \ 0xy": There is an escape character '\' (backslash). In this way, the following string "0xy" is still calculated, the length of the string is 7 (instead of combining the second backslash with the subsequent 0 as an escape character '\ 0'. Otherwise, the first backslash cannot be processed, because an escape character is always composed of a backslash and other characters, a single backslash cannot be any valid character ).
(5) If you change the string "ABC \ 0xy" to "ABC \ 0xy", there are two escape characters '\' (backslash) and '\ 0' (string Terminator). When the strlen function is used to test the length of the string, the result is 4 (instead of 7 ).
(6) If you change the string "ABC \ 0xy" to "ABC \ 061xy", there are two escape characters '\' (backslash) and '\ 061' (ASCII code value is equal to 061 characters, that is, the number character '1'). When the strlen function is used to test the length of the string, the result should be 7 (instead of 4 or 9 ). Therefore, when you encounter an escape character '\ 0', you must check whether there are any numbers behind it. If so, you must add one to two numbers) it is combined with '\ 0' in front of it as a character to calculate the length of the entire string.
If printf ("ABC \ 061xy") is used, the output result is: ABC \ 1xy.