Why output of int 070 is 56 in C program? [duplica

2020-02-02 03:44发布

Can you explain it? Why it given 56 value as output?

#include <stdio.h> 
#include <conio.h>

void main()
{
    int x = 070;
    printf("%d", x);
    getch();
}

1条回答
萌系小妹纸
2楼-- · 2020-02-02 04:08

Any integer literal (integer constant) starting with 0 is an octal representation.

Quoting C11, chapter §6.4.4.1, Integer constants

octal-constant:

 0
octal-constant octal-digit

and

octal-digit: one of

  0 1 2 3 4 5 6 7

and, as per chapter §7.21.6.1, for %d format specifier with printf(), (emphasis mine)

d,i The int argument is converted to signed decimal [...]

Thereby, octal 70 == decimal 56.

查看更多
登录 后发表回答