Converting decimal to binary

This is a discussion on Converting decimal to binary within the C Programming forums, part of the General Programming Boards category; Hello. I'm encountering some errors in my code. Please help. Code: void binary(int number) { int i, count; for(count = ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    9

    Converting decimal to binary

    Hello. I'm encountering some errors in my code. Please help.

    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

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    9
    But I'm using Visual Studio 2005 Express Edition.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    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.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    7

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Decimal to Binary
    By BeldenML in forum C Programming
    Replies: 17
    Last Post: 02-09-2012, 10:29 AM
  2. arrays and converting decimal to binary
    By kt1991 in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2011, 02:13 PM
  3. converting decimal to binary in stack
    By Cpe Engeya in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2009, 07:24 PM
  4. Converting decimal to binary
    By ubernos in forum C Programming
    Replies: 3
    Last Post: 12-06-2005, 09:09 AM
  5. Converting decimal to binary?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21