代码简单,仅做记录。
/* 递归求二进制 */
#include<stdio.h>
#include<string.h>
int b_d (char *a, int x)
{
static int i = 0;
int j = 0;
if(x == 0)
return x%2;
else
{
j = (b_d(a, x/2) + 48);
a[i++] = j;
return x%2;
}
}
int main(void)
{
char a[10] = {0};
char b[2] = {0};
*b = (b_d(a, 10)+48);
strcat(a,b);
printf("%s\n", a);
return 0;
}
另外,我写了b[1],但发现输出了5个长度,最后发现该数组没法存储终止符。