在C中扫描和打印可变数据类型“double”

我正在学习算法,很想学习。 我的问题是,我不能使用C中的整数数据类型输出数字“600851475143”。所以我切换到双打。 但是输出是“0.0000”。 有人可以告诉我我做错了什么吗? 我只是想扫描并打印双数据类型中的任何数字,然后我将重点放在获取最高的主要因素:)

#include <stdio.h>
#include <math.h>

int main()
{
double number;
double div = 2;
double highest = 2;

printf("Please input a number: ");
scanf("%lf", &number);

printf("n You entered: %lf", &number);

while(number!=0){
    if(fmod(number,div) != 0){div = div + 1;}
    else{
    number = number / div;
    printf("n %lf", div);

    if(highest < div){highest = div;}
    if(number == 1){break;}
    }
}
printf("n The highest number is %lf", highest);

return 0;}

我做了什么:

  • 在google搜索“scan double in c”,了解到“%lf”是正确的路要走,但程序没有显示任何内容。

  • 我检查了Stackoverflow中的各种问题,如:

  • 为什么scanf需要用于双打

    用c中的scanf读取double值

    浮动和双重之间的区别

    在C文件中读取和写入(加倍)

    从文件读取和写入双精度

    浮点与双精度

    用c中的scanf读取double值

  • 我搜索的其他网站:
  • http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html

    谢谢你用你的知识来启发我。


    用带警告的编译器进行编译显示了这个问题:

    dbl.c:13:31: warning: format specifies type 'double' but the argument has type
         'double *' [-Wformat]
    printf("n You entered: %lf", &number);
                           ~~~   ^~~~~~~
    1 warning generated.
    

    将该行更改为:

    printf("n You entered: %lf", number);
    

    您正在尝试打印号码的地址而不是号码。

    另外,尝试在最后添加一个换行符:

    printf("n The highest number is %lfn", highest);
    

    因为,你正在使用整数,但是,我建议你只用很long long ,假设你有一个C99编译器:

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    long long number;
    long long div = 2;
    long long highest = 2;
    
    printf("Please input a number: ");
    scanf("%lld", &number);
    
    printf("n You entered: %lld", number);
    
    while(number!=0){
        if(number % div != 0){div = div + 1;}
        else{
        number = number / div;
        printf("n %lld", div);
    
        if(highest < div){highest = div;}
        if(number == 1){break;}
        }
    }
    printf("n The highest number is %lldn", highest);
    
    return 0;}
    

    为了可读性,您还可以重新格式化您的代码:

    #include <stdio.h>
    #include <math.h>
    
    int main() {
        long long number, div, highest;
        div = highest = 2;
    
        printf("Please input a number: ");
        scanf("%lld", &number);
    
        printf("n You entered: %lld", number);
    
        while(number != 0) {
            if (number % div != 0) div += 1;
            else {
                number /= div;
                printf("n %lld", div);
    
                if (highest < div) highest = div;
                if (number == 1) break;
            }
        }
        printf("n The highest number is %lldn", highest);
    
        return 0;
    }
    

    我还建议换行符略有不同,这会给你留下:

    #include <stdio.h>
    #include <math.h>
    
    int main() {
        long long number, div, highest;
        div = highest = 2;
    
        printf("Please input a number: ");
        scanf("%lld", &number);
    
        printf("n You entered: %lldn", number);
    
        while(number != 0) {
            if (number % div != 0) div += 1;
            else {
                number /= div;
                printf("%lldn", div);
    
                if (highest < div) highest = div;
                if (number == 1) break;
            }
        }
        printf("The highest number is %lldn", highest);
    
        return 0;
    }
    

    问题出在函数printf("%lf",&number)应该是printf("%lf",number)

    链接地址: http://www.djcxy.com/p/78631.html

    上一篇: Scanning and printing variable data type "double" in C

    下一篇: Why does the addition of two float numbers is incorrect in C?