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

孤街浪徒 提交于 2019-11-28 13:55:39

问题


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:


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.



来源:https://stackoverflow.com/questions/36650007/why-output-of-int-070-is-56-in-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!