使用C将整数转换为二进制表示吗?

0 c binary

我仍然是编码的初学者,所以遇到了这个问题,我正在尝试将整数转换为二进制表示形式

 #include <stdio.h>
 int main () {
   int x;
   printf("input the number\n");
   scanf("%d",&x);

   while(x!=0) {
     if (x%2)
       printf("1");
     else
       printf("0");
   }
   return 0;
 }
Run Code Online (Sandbox Code Playgroud)

所以它的输出像这样12 = 0011但12 = 1100这是什么问题,我该如何解决?

Bar*_*mar 5

该操作的程序逻辑错误,请尝试此操作

#include <stdio.h>
int main()
{
    int n, c, k;

    printf("Enter an integer in decimal number system\n");
    scanf("%d", &n);
    printf("%d in binary number system is:\n", n);

    for (c = 31; c >= 0; c--)
    {
        k = n >> c;

        if (k & 1)
            printf("1");
        else
            printf("0");
        }

        printf("\n");
    } 
    return 0;
}
Run Code Online (Sandbox Code Playgroud)