C中扫描打印可变数据类型"double"

Scanning and printing variable data type "double" in C

我正在研究算法并且很想学习。我的问题是,我无法使用 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;}

我做了什么:

  1. 在google中搜索"scan double in c",得知“%lf”是正确的方法,但程序没有显示任何内容。

  2. 我在 Whosebug 中查看了各种问题,例如:

Why does scanf need lf for doubles

Reading in double values with scanf in c

Difference between float and double

Read and Write within a file in C (double it)

Reading and writing double precision from/to files

float vs. double precision

Reading in double values with scanf in c

  1. 我搜索的其他网站:

http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html

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

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

使用带有警告的编译器编译显示问题:

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 %lf\n", 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 %lld\n", 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 %lld\n", 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\n", number);

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

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

    return 0;
}