Search code examples
cdecimaloctal

When displaying the value of variable "int a = 011", I get 9. Why?


With this code snippet:

int a = 011;
printf("a = %d", a);

Why is the result

a = 9


Solution

  • 011 is an octal value and its decimal equivalent is 9. Preceding integer literal with 0 indicates octal value.
    Use %o specifier in printf to print the value in octal.