-
Double Data Type
I am new to the C programming language, so, bare with me.
Everytime I run this code I get a whole number and not a decimal number.
Am I missing something?
Code:
#include <stdio.h>
main()
{
double pi;
printf("Enter the value of PI:");
scanf("%d", &pi);
printf("Pi is %d\n", pi);
fflush(stdin);
getchar();
}
-
becase you dont print it in a right format. use this
Code:
#include <stdio.h>
int main()
{
double pi;
int ch;
printf("Enter the value of PI:");
scanf("%lf", &pi);
printf("Pi is %lf\n", pi);
while((ch=getchar()) != '\n' && ch != EOF);
getchar();
return 0;
}
And dont use fflush(stdin) seeFAQ
ssharish2005
-
I wouldn't recommend using %lf to print out a double; it's valid in C99 but is not defined in C89. %f works for printing doubles in both C89 and C99 and thus will be more portable.
%lf is still correct for doubles and scanf(), however.