The length of the string containing \ 0

char p[]="abc\012\0x34"; printf("%d\n",strlen(p)); 

I get output 4. Shouldn't it be 3 ??? Although for the next I get 3.

  char p[]="abc\0"; printf("%d\n",strlen(p)); 
+4
source share
5 answers

Your line contains four characters before \0 , i.e. abc and \012 .

The latter is a valid octal escape sequence that is 10 in decimal value, i.e. an ASCII line feed character.

\0x34 , on the other hand, is not valid octal - only the \0 part is valid, so the real end of your completed NUL string.

+12
source

\012 is an octal escaped character, not a NUL , followed by 1 and 2 . x terminates the second octal character, so it really is NUL . ( \x34 will be the correct form for the hexadecimal escaped character.)

Representing the NUL as \0 is just a special case of the octal escape sequence. In the general case, \ can be followed by one, two, or three octal digits to form a valid octal escape sequence in a character or string literal.

+4
source

Your string has a length of 4:

This code is equivalent: char p [] = {'a', 'b'. 'c'. '\ 012', '\ 0', 'x', '3', '4', '\ 0'};

\ 012 - character with code 12 in the octal system of digits (= 10 in decimal = '\ n')

+4
source

\012 - one character. After that, it stops at \0 (and "x34" is another three characters, not counting the NUL terminator).

+1
source

\012 is the octal value ("\ n").

0
source

All Articles