-2
int n = 00000011;
printf("n is: %d\n", n);

x is: 9

Shouldn't it be 3 in decimal?

CodeCrack
  • 5,253
  • 11
  • 44
  • 72
  • 1
    possible duplicate of [Is 0 a decimal literal or an octal literal?](http://stackoverflow.com/questions/6895522/is-0-a-decimal-literal-or-an-octal-literal) – Rizier123 Jan 06 '15 at 21:13
  • 1
    in C numbers starting with 0 are octal numbers. – Jasen Jan 06 '15 at 21:13

2 Answers2

4

00000011 is the octal value of 9, you can't use the binary representation directly.

Octal 011 -> Decimal 9

Only, decimal, octal and hexadecimal representations can be specified, for the decimal representation it's straight forward, for the octal representation you prefix the value with a 0 so 09 would not be valid, and for hex representation you prefix the value with 0x.

note: as commented by abligh, you can use the 0b prefix with gcc and clang and probably other compilers.

abligh
  • 24,573
  • 4
  • 47
  • 84
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 2
    Actually he's *printing* the decimal value of `n`, but setting `n` to be octal 011, i.e. 9 (in decimal). IE it's the assignment not the printing that is decimal. – abligh Jan 06 '15 at 21:12
  • 2
    In gcc (and I think other C compilers) you can represent binary constants by `int n = 0b11;` as per https://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html – abligh Jan 06 '15 at 21:16
  • @abligh I didn't know that, it's not standard right? – Iharob Al Asimi Jan 06 '15 at 21:17
  • so for example if I wanted to count the number of bits in a number. Would I represent n as a decimal then? int n = 3 instead of n = 0000011 – CodeCrack Jan 06 '15 at 21:17
  • @CodeCrack the representation has nothing to do with counting bits. – Iharob Al Asimi Jan 06 '15 at 21:18
  • @iharob it's a gcc extension, but other compilers (e.g. clang: http://clang.llvm.org/docs/LanguageExtensions.html) also support it. – abligh Jan 06 '15 at 21:18
  • @iharob I understand but let's say I am counting bits using count += (x & 1); x = x >> 1;. I want to specify 00000011 as input. Would I just pass 3 as decimal input instead then? – CodeCrack Jan 06 '15 at 21:19
  • @CodeCrack Yes, or `0x03` hex, or `03` octal. – Iharob Al Asimi Jan 06 '15 at 21:20
  • @iharob, no the assigment is binary from an octal literal, the number in the CPU is in binary, and really looks like 00000000 00000000 00000000 00001001 printf converts that to base 10 – Jasen Jan 06 '15 at 21:21
  • 1
    @CodeCrack the important thing to note is that how the number got there in the first place is irrelevant. You could have written `n=0x03;` or `n=3;` or `n=03;` or `n=0b11` (on gcc) or `n=517-514;` - once the value of n has been assigned, it's held in memory. That internal storage is always binary (ones and zeros) – abligh Jan 06 '15 at 21:22
0

In c number literals starting with 0*digit* are base-8. For binary some compilers allow you to start them with 0b. else you'll have to manually convert them to hex or octal.

#include <stdio.h>
int main(void){
    int n=0b00000011;
    printf ("n is %d\n",n);
    return 0;
};
Jasen
  • 11,837
  • 2
  • 30
  • 48
  • 1
    Note `0b` is a gcc extension which is supported by some other C compilers (e.g. clang), and not part of the C standard. – abligh Jan 06 '15 at 21:19