1

Consider the C code below:

#include <stdio.h>
int main()
{
    int i = 012;
    printf("%d", i);
}

On O/P the value of i is printed as 10. I know (012)8 = (10)10. But I'm confused about how C stores int variable values. Can anybody explain it to me?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Atinesh
  • 1,790
  • 9
  • 36
  • 57
  • 6
    You set `i` to octal 012, and `printf()` it in decimal (`%d`), which is 10. What's so confusing? – EOF May 21 '15 at 17:18
  • 1
    There is no `c` in your code. – Kerrek SB May 21 '15 at 17:19
  • @KerrekSB-LOL, OP is talking about the C language. – Am_I_Helpful May 21 '15 at 17:20
  • 2
    Kind of depends on your computer...on most it stores them dynamic RAM, which is small capacitors charged at different levels for 0 and 1 bits. Some computers have static RAM, which are bi-stable arrangements of transistors whose two states represent 0 or 1. After writing to disk, they are domains of magnetic charge on oxide-coated aluminum. – Lee Daniel Crocker May 21 '15 at 19:25

4 Answers4

7

They are stored in binary, you can use many representations to define them, but in the end it's the binary representation that is used.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

Hardware memory operates with bytes. So any variable (int, char, pointer, or array of those) in your program in the end will be converted to bytes. How "C" will do that again depends on hardware. See Endianness and Size of int long type.

Say you have 32-bit little-endian computer. Any 'int' in 32-bit world will be stored as four consecutive bytes in memory. That means in your case whatever literal will you use, is it either '012' or '10', it will be stored like this:

binary:  00001010 00000000 00000000 00000000
octal:         12        0        0        0
decimal:       10        0        0        0
hex:            A        0        0        0

in four consecutive bytes of memory.

Community
  • 1
  • 1
fukanchik
  • 2,811
  • 24
  • 29
0

All values are stored using binary representation (this is true for integers, floats, characters, etc). The value 10 (decimal) is stored as the sequence of binary digits 00000000000000000000000000001010 (assuming a 32-bit int type).

In an assignment statement like

i = 012;

the compiler knows how to convert the string of octal digits in the literal 012 into the binary representation above. The leading 0 tells the compiler that the value is in octal representation, otherwise it would try to store the decimal value 12 (00000000000000000000000000001100).

In the printf statement

printf("%d\n", i);

the conversion specifier %d tells printf to display the value stored in i formatted as a string of decimal digits (10). Similarly, the conversion specifier %x will display the value formatted as a string of hex digits (a).

John Bode
  • 119,563
  • 19
  • 122
  • 198
-3

To the best of my knowledge it is the C integer handler that is seeing the leading 0, and then interpreting it as base 8, or an octal constant.

Sorry for simply redirecting you, but I fear I could not say it any better than Abimaran Kugathasan in this post:

Why int j = 012 giving output 10?

For a deeper explanation, see the answer by bdonlan here: How does C Handle Integer Literals with Leading Zeros, and What About atoi?

Community
  • 1
  • 1
Nick
  • 315
  • 2
  • 9
  • 2
    The OP claims to know that it's an octal value, also this could be a comment instead of an answer, because after all you are redirecting to other posts in SO, which means that this question could be marked as duplicate. – Iharob Al Asimi May 21 '15 at 17:24
  • 1
    I can't comment yet I don't have enough rep :( – Nick May 21 '15 at 17:33
  • Still your answer accepted and everything does not answer the question, and it's also a link only answer. The fact that an answer is accepted doesn't mean it has te be upvoted. – Iharob Al Asimi May 21 '15 at 18:32