-3

In the below code ,variable 'b' holds value '133' which is quite straight forward. How about variable 'a' ? Why is it '131' ?

I see the only difference is '015' instead of '15'.

#include<stdio.h>
int main()
{
  int a,b,c;
  a=015 + 0x71 +5;
  printf("%d\n",a);  // prints '131'
  b=15 + 0x71 +5;
  printf("%d\n",b);  // prints '133'
}

Can someone let me know whats going on in here ?

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
shwink
  • 309
  • 2
  • 12
  • **hexadecimal** = base 16 = `0x{9-9A-Fa-f_]+`, every "digit" represents 4 bits, **octal** = base 8 = `0[0-7_]+`, every digit represents 3 bits. 015 is octal. Hexadecimal has won in computer history. – Joop Eggen Nov 27 '15 at 10:31

2 Answers2

7

Leading zeros indicate that the number is expressed in octal. 015 in octal is 13 in decimal notation.

octal-literal is the digit zero (0) followed by zero or more octal digits (0, 1, 2, 3, 4, 5, 6, 7)

(from cppreference)

Chris Maes
  • 35,025
  • 12
  • 111
  • 136
3

015 is actually a indicator for octal number system representation, so

015 means 1x8^1 + 5*8^0 = 13

So

  a=015 + 0x71 +5;

Strictly speaking means (1*(8^1) + 5*(8^0) ) + (7*(16^1) + 1*(16^0)) + (5*(10^0)) = 133

Magisch
  • 7,312
  • 9
  • 36
  • 52