Scanning and printing variable data type "double" in C
I'm studying algorithms and would love to learn. My problem is this, I can't output a number "600851475143" using the integer data type in C. So I switched to doubles. However the output is "0.0000". Can someone kindly tell me what I'm doing wrong? I simply want to scan and print any number in the double data type then I'm going to focus on getting the highest prime factor :)
#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;}
What I did:
Searched for "scan double in c" in google, learned the "%lf" is the right way to go, but the program doesn't show anything.
I checked out various questions in Stackoverflow like:
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
http://www.technoburst.net/2011/07/reading-double-in-c-using-scanf.html
Thank you for enlightening me with your knowledge.
Compiling with a compiler with warnings shows the issue:
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.
Change that line to:
printf("n You entered: %lf", number);
You're trying to print the number's address instead of the number.
Also, try adding a newline to the end:
printf("n The highest number is %lfn", highest);
Since, you're working with integral numbers, though, I'd advise you to just use a long long
, assuming you have a C99 compiler:
#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;}
You can also reformat your code a bit for readability:
#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;
}
I would also advise doing the newlines a little differently, which leaves you with:
#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)
上一篇: 选择数据类型以及如何理解它们