# Converting decimal to binary

• 03-03-2012
BrandNewDeipz
Converting decimal to binary

Code:

```void binary(int number) {         int i, count;         for(count = 0; number > 0; count++)         {                 number /= 2;         }         int remainder[count];         for(i = 0; i < count; i++)         {                                 remainder[i] = number % 2;                 printf("%d\t", remainder);                 number /= 2;                 printf("%d\t", number);                 printf("%d\n\n", i);         }         for(i = count; i >= 0; i--)                 printf("%d", remainder[1]); }```

Errors:
1. expected constant expression
2. cannot allocate an array of constant size 0
3. 'remainder' : unknown size
• 03-03-2012
grumpy
The 1989 C standard did not support variable size arrays (a-la "int remainder [count];" in your code). The 1999 C standard did.

This means, depending on age of your C compiler, that your code is invalid.

Either update your compiler, or look up use of malloc() to dynamically allocate memory, and free() to release it.
• 03-03-2012
BrandNewDeipz
But I'm using Visual Studio 2005 Express Edition. :(
• 03-03-2012
grumpy
It typically takes up to ten years after a standard is released before you can be confident that new compilers implement it reasonably completely.

Visual Studio only partly implemented features of of the 1999 C standard until at least 2008. 2005 was before 2008, so Visual Studio 2005 editions would also have only had partial support for C99.
• 03-03-2012
getnitin15
can try this..
Code:

```void binary(int number) {     int i, count, temp = number;     for(count = 0; number > 0; count++)     {         number /= 2;     } number =temp; // try saving the input.. else you would have lost it in the first loop. int * remainder =  calloc(count, sizeof(int)); //to get around your compiler //incompatibility. should use free though as stated.     for(i = 0; i < count; i++)     {                 remainder[i] = number % 2;         printf("%d\t",remainder[i]);         number /= 2;         printf("%d\t", number);         printf("%d\n\n", i);     }     for(i = count - 1; i >= 0; i--) // introduce a -1 here prints an extra zero..         printf("%d", remainder[i]); }```
hope this helps..

Nitin