c - 我是 C 新手。第二个 for 循环永远不会结束为什么会这样

标签 c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    float  n ,a, sum,count,count1;
    printf("\nInput integer in base 10 : ");
    scanf("%f", &n);
    for(count=0 ; count<=100 ; ++count){
        if(n/pow(2,count)<2 && n/pow(2,count)>=1)break;
    }
    a=n-pow(2,count);
    sum=pow(10,count);
    for(count1=count-1 ; count1>=0 ; --count){
        if(a/pow(2,count1)<2 && a/pow(2,count1)>=1 ){
            a-=pow(2,count1);
            sum+=pow(10,count1);
        }
    }
    printf("The binary of %.0f is %.0f",n,sum);
    return 0;
}

此代码用于在不使用数组的情况下打印十进制数的二进制等价物

最佳答案

正如 @kiner_shah 在对您的问题的评论中所述,您在第二个循环中减少了一个不正确的变量。

for(count1=count-1 ; count1>=0 ; --count){
    ...

应该是

for(count1=count-1 ; count1>=0 ; --count1){
    ...

使用更清晰/描述性的变量名称来避免单字符错误是一个好习惯。

关于c - 我是 C 新手。第二个 for 循环永远不会结束为什么会这样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847500/

相关文章:

c - 我如何理解该程序的输出?

c - 关于动态代码分析的任何引用资料?

c - 中断服务例程不会跳回到 ARM Cortex M0 上的中断处理程序

c - 当您不知道字符串有多长时,从 C 中的用户输入中拆分字符串

c - popen 函数 : Command not found or exited with error status in C program

c 程序时间转换 24 小时到 12 小时

c - memcpy,段错误

c - 开始使用 ARMCI

c - 在读取内存时递增变量

c++ - 有趣的问题(货币套利)