Octal representation inside a string in C

后端 未结 2 1930
迷失自我
迷失自我 2020-12-19 19:02

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]);
           


        
2条回答
  •  梦毁少年i
    2020-12-19 19:27

    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

提交回复
热议问题