-1

I got unexpected outputs, and I could not figure out the reason.

    #include <stdio.h>

    int main() {
        int a = 0100;
        int b = 010;
        int c = 1111;
        int d = 01111;
        printf("0100 => %d, 010 => %d, 1111 => %d, 01111=> %d\n", a, b, c, d);
   }

Output:

0100 => 64, 010 => 8, 1111 => 1111, 01111=> 585 

Why does such output occur?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
MKS
  • 199
  • 1
  • 8
  • Any [C tutorial](https://www.tutorialspoint.com/cprogramming/index.htm) gives the answer. Or the [C11 reference](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). Or the [C syntax](https://en.wikipedia.org/wiki/C_syntax) wikipage. Or [this C reference](https://en.cppreference.com/w/c). Also read [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Basile Starynkevitch Dec 07 '19 at 11:38
  • I would like to highlight to viewers that this is actually a great question. Bit manipulation is not something that all programmers are strong with, and knowing the conventional hex/octal prefixes for integers is not exactly "fundamental" across the board. – CinchBlue Dec 07 '19 at 11:46

2 Answers2

5

In C, putting either 0x or 0 before an integer literal value will cause the compiler to interpret the number in base 16 (hexadecimal) and base 8 (octal), respectively.

Normally, we interpret and read numbers in base 10 (decimal). However, we sometimes will use these other bases because they are useful powers of 2 that can represent groups of bits (1 hexadecimal digit = 4 bits, 1 octal digit = 3 bits), and bit manipulation is something that C wants to provide. This is why you'll see something like char be represented with 2 hexadecimal digits (e.g. 0x12) to set a single char to be a specific bit sequence.

If you wanted to verify this, printf also allows you to output int in hexadecimal and octal as well using %x and %o respectively instead of %d.

#include <stdio.h>
int main()
{
    int a = 0100;
    int b = 010;
    int c = 1111;
    int d = 01111;
    printf("0100 => %o, 010 => %o, 1111 => %d, 01111=> %o\n", a,b,c,d);
}

If you run this program, you'll get the following:

gcc -ansi -O2 -Wall main.c && ./a.out
0100 => 100, 010 => 10, 1111 => 1111, 01111=> 1111

...which is exactly what you set the values to in the program, just without the prefixes. You just mistakenly used another integer encoding by accident on assignment in the original code, and used a different one to output the value.

CinchBlue
  • 6,046
  • 1
  • 27
  • 58
3

0 prefix gives you octal, and you tried to print decimal.

Van Tr
  • 5,889
  • 2
  • 20
  • 44