-1

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();
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37
  • If the language was being designed these days, I wonder if they'd still use the `0` prefix for octal? I think my favored alternative would be `0k`/`0K`. (`0o` or `0O` would be too confusing.) – Ian Abbott Apr 15 '16 at 14:45

1 Answers1

16

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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261