4

In the given program:

int main() {
  char *p = "\0777";
  printf("%d %d %d\n",p[0],p[1],p[2]);
  printf("--%c-- --%c-- --%c--\n",p[0],p[1],p[2]);
  return 0;  
}

It is showing the output as:

63 55 0
--?-- --7-- ----

I can understand that it is converting the first two characters after \0 (\077) from octal to decimal but can any one explain me why 2 characters, why not 1 or 3 or any other ?

Please explain the logic behind this.

r.bhardwaj
  • 1,603
  • 6
  • 28
  • 54

2 Answers2

5

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.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
5
char *p = "\07777";

Here a string literal assigned to a pointer to a char.

"\07777"

In this string literal octal escape sequence is used so first three digits represents a octal number.because rules for octal escape sequence is---

You can use only the digits 0 through 7 in an octal escape sequence. Octal escape sequences can never be longer than three digits and are terminated by the first character that is not an octal digit. Although you do not need to use all three digits, you must use at least one. For example, the octal representation is \10 for the ASCII backspace character and \101 for the letter A, as given in an ASCII chart.

SO your string literal stored in memory like

1st byte as a octal number 077 which is nothing but 63 in decimal and '?' in character

2nd and 3rd byte as a characters '7' and '7' respectively

and a terminating character '\0' in last.

so your answer are as expected 1st,2nd,3d byte of the string literal.

for more explanation you can visit this web site

http://msdn.microsoft.com/en-us/library/edsza5ck.aspx

rajesh6115
  • 705
  • 9
  • 21
  • this link give you more on escape sequences http://c0x.coding-guidelines.com/6.4.4.4.html – rajesh6115 Aug 05 '12 at 11:46
  • then what is your question.If you clearly mention then only a good response you can get. – rajesh6115 Aug 05 '12 at 16:13
  • to be more precise octal numbers should be in a range from "\000" to "\377" because it will stored in 1 byte and after "\377" it will show warning: octal escape sequence out of range. – r.bhardwaj Aug 06 '12 at 10:47
  • yes,the octal escape sequences can be max to "\377" but if you provide "\378" then "\37" is your octal number and 8 is character(that is max 3 character provided all are between 0 to 7).Are you clear about this concept or not? – rajesh6115 Aug 06 '12 at 13:13