将十进制转换为二进制的C程序

将十进制转换为二进制的C程序,c,string,algorithm,gcc,integer,C,String,Algorithm,Gcc,Integer,我已经用C写了一个程序,将十进制数转换成二进制数并存储在字符串中。问题不在于反向打印的二进制文件,而在于如何显示输出 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int decimal; // get the decimal from user printf("Enter the decimal number: "); sca

我已经用C写了一个程序,将十进制数转换成二进制数并存储在字符串中。问题不在于反向打印的二进制文件,而在于如何显示输出

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    int decimal;

    // get the decimal from user
    printf("Enter the decimal number: ");
    scanf("%i", &decimal);

    if(decimal == 1)
        printf("The binary equivalent of %i is %i\n", decimal, decimal);
    else
    {
        // considering binary is 64 bits in total
        char bit[64];
        int dividend, remainder, counter;

        // we need decimal value intact so dividend copies it
        dividend = decimal;
        counter = 0;
        do
        {
            remainder = dividend % 2;
            if(remainder != 0)
                bit[counter] = putchar('1');
            else
                bit[counter] = putchar('0');
            // update the dividend and counter
            dividend /= 2;
            ++counter;
            // break if dividend has reached 1
            if(dividend == 1)
                break;
          } while(dividend);

          // print the final result
          printf("The binary equivalent of %i is %s\n", decimal, bit);
    }

  return(0);
}
2的输出值应该是01,反之亦然 $0 2的二进制等价物是0次 小数点3 $1二进制等价物3是1时间

您需要更改

  if(dividend == 1)


在使用之前,也将memset bit设置为0,即memsetbit,“\0”,sizeofbit;在循环之前,我们初学者应该互相帮助

给你

#include <stdio.h>

int main(void) 
{
    unsigned int decimal;

    // get the decimal from user
    printf("Enter the decimal number: ");
    scanf("%u", &decimal);

    // considering binary is 64 bits in total
    char bit[64];
    unsigned int dividend, remainder, counter;

    // we need decimal value intact so dividend copies it
    dividend = decimal;
    counter = 0;

    do
    {
        remainder = dividend % 2;
        bit[counter++] = remainder + '0';
    } while( dividend /= 2 );

    bit[counter] = '\0';

    // print the final result
    printf("The binary equivalent of %u is %s\n", decimal, bit);

    return 0;
}
至于你的代码,那么这个代码片段

if(decimal == 1)
    printf("The binary equivalent of %i is %i\n", decimal, decimal);
else
        if(remainder != 0)
            bit[counter] = putchar('1');
        else
            bit[counter] = putchar('0');
这是多余的

此代码段

if(decimal == 1)
    printf("The binary equivalent of %i is %i\n", decimal, decimal);
else
        if(remainder != 0)
            bit[counter] = putchar('1');
        else
            bit[counter] = putchar('0');
没有道理。正如上面的演示程序所示,您需要的是编写

bit[counter++] = remainder + '0';
这是循环的出口

        if(dividend == 1)
            break;
这是错误的

您还需要将结果字符串附加一个终止零

另外,由于您没有考虑输入数字的符号,因此最好将其声明为具有类型unsigned int


考虑到头和是冗余的,可能会被删除。

使用所有警告和调试信息(例如gcc-Wall-Wextra-g)编译,然后使用调试器gdb,因此花几个小时学习如何使用它。顺便说一句,您的“修复我的代码”请求与主题无关。调试是编程活动的一个重要部分;做首先,它输出“1”并返回其ASCII值,但我认为这不是您想要的至少不是输出部分。@MichaelWalz 1base10=1base2,3base10=11base2。我不是指控制台屏幕上的确切文字。因此,如果用户输入3,则输出应为11。4将是001,也就是100。但我想保持相反的方式。@BenSteffan是否应该使用strcatbit,1?@KeyikedalubeNdang否。只需写入位[counter]=“1”;。我不知道您对将值写入数组的困惑是从何而来的,但由于您似乎并不打算产生任何副作用,例如将字符串写入stdout,所以只需将char值保存在缓冲区中就足够了。我在代码中犯了太多错误。莫斯科的弗拉德指出了我……:而你们做了我想做的事。谢谢:我可以用GPL许可这段代码,以防我把它分享给我的朋友。当然,来自莫斯科的弗拉德将是作者。。。请?@KeyikedalubeNdang您可以自由使用此代码: