Understanding output of printf containing backslash (\012)

The character sequence \012 inside the string is interpreted as an octal escape sequence. The value 012 interpreted as octal is 10 in decimal, which is the line feed (\n) character on most terminals.

From the Wikipedia page:

An octal escape sequence consists of \ followed by one, two, or three octal digits. The octal escape sequence ends when it either contains three octal digits already, or the next character is not an octal digit.

Since your sequence contains three valid octal digits, that’s how it’s going to be parsed. It doesn’t continue with the 3 from 34, since that would be a fourth digit and only three digits are supported.

So you could write your string as "55\n34", which is more clearly what you’re seeing and which would be more portable since it’s no longer hard-coding the newline but instead letting the compiler generate something suitable.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)