扫描和打印可变数据类型" double"在C.

时间:2015-02-18 21:49:22

标签: c printf double scanf

我正在学习算法并且很乐意学习。我的问题是这个,我不能使用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. 在谷歌搜索“扫描double in c”,了解到“%lf”是正确的方法,但程序没有显示任何内容。

  2. 我在Stackoverflow中检查了各种问题,如:

  3. 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. 我搜索过的其他网站:
    2. http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html

      感谢您用您的知识启发我。

2 个答案:

答案 0 :(得分:1)

问题在于功能printf("%lf",&number)应为printf("%lf",number)

答案 1 :(得分:0)

使用带警告的编译器进行编译会显示问题:

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;
}
相关问题