【发布时间】:2017-04-29 15:35:45
【问题描述】:
我用 C 语言编写了以下代码,以使用函数查找数字的二进制。
//C program to find the binary equivalent of a number using functions
#include <stdio.h>
void bineq(int);
void main()
{
int a=5,b=3; //Initialize two numbers
printf("The binary equivalent of a= \n");
bineq(a);
printf("The binary equivalent of b= \n");
bineq(b);
}
void bineq(int x) // Function Definition.
{
int p[50];
int i=0,t,j;
t=x;
while(t>=0)
{
p[i]=t%2;
i++;
if(t==0){
break;}
t=t/2;
}
for(j=i-1;j>=0;j--)
{
printf("%d ",p[i]);
}
}
当我运行代码时,我得到以下值:
The binary equivalent of a= -1610823072 -1610823072 -1610823072 -1610823072
The binary equivalent of b= 32644 32644 32644
当我在C Tutor(在线编译器)中运行代码时,bineq函数中的printf语句出现如下错误:
ERROR: Conditional jump or move depends on uninitialized value(s) (Stopped
running after the first error. Please fix your code.)
5 的二进制正确存储在整数数组 p 中,但我无法向后显示每个整数。
我想以最简单的方式解决问题。我是否遗漏了代码中的任何逻辑或任何一行?
【问题讨论】: