It's just the way the language defines octal escape sequences.
An octal escape sequence, which can be part of a character constant or string literal, consists of a \
followed by exactly 1, 2, or 3 octal digits ('0'
.. '7'
).
In "\07777"
, the backslash is followed by 3 octal digits (0, 7, 7), which represents a character with the value 077
in octal, or 63
in decimal. In ASCII or an ASCII-derived encoding, that happens to be a question mark '?'
.
So the literal represents a string with a length of 3, consisting of '?'
, '7', '7'.
But there must be a typo in your question. When I run your program, the output I get is:
63 55 55
--?-- --7-- --7--
If I change the declaration of p
to
char *p = "\0777";
I get the output you describe. Note that the final ----
is really two hyphens, followed by a null character, followed by two hyphens. If you're on a Unix-like system, try piping the program's output through cat -v
or cat -A
.
When you post code, it's very important to copy-and-paste it, not retype it.
(And you're missing the #include <stdio.h>
at the top.)