I am trying to read in a double value continuously from the user using scanf.
Code:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
The output is
INPUT: 0.000
You lied to the compiler: when scanning, %f says you supply a pointer to float. But you provided a pointer to double.
To fix, either use %lf or declare input as float.
Note that there is an asymmetry with printf formats, which uses %f for both float and double arguments. This works because printf arguments are promoted to double (and are not pointers).
I am trying to read in a double value continuously from the user using scanf.
To do so you need a loop, like the following:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
Note that, as the primitive type of input is double, you need to use %lf instead of %f (%fused for float).
Related
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
I try to read-in 2 values using scanf() in C, but the values the system writes into memory are not equal to my entered values. Here is the code:
double a,b;
printf("--------\n"); //seperate lines
scanf("%ld",&a);
printf("--------\n");
scanf("%ld",&b);
printf("%d %d",a,b);
If I enter 1 and 2, CMD returns a correct, but b = -858993460 Here is what I already tried:
using float or int instead of double, using scanf_s, using scanf("%d or %f for %i or %li or %lf or %e or %g ), using fflush(stdin) to clear keyboard buffer, reading in b first, trying like all possible combinations. I found out that there is a problem with the length of double on 32 bit OS, so that you are forced to use scanf("%lf", &f) to read in a double. No matter what I do, second value is always wrong.
I use MS VS express 2012 for Desktop on Windows 7 32 bit OS.
Use the %lf format specifier to read a double:
double a;
scanf("%lf",&a);
Wikipedia has a decent reference for available format specifiers.
You'll need to use the %lf format specifier to print out the results as well:
printf("%lf %lf",a,b);
As far as i know %d means decadic which is number without decimal point. if you want to load double value, use %lf conversion (long float). for printf your values are wrong for same reason, %d is used only for integer (and possibly chars if you know what you are doing) numbers.
Example:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a);
printf("--------\n");
scanf("%lf",&b);
printf("%lf %lf",a,b);
You are using wrong formatting sequence for double, you should use %lf instead of %ld:
double a;
scanf("%lf",&a);
Format specifier in printf should be %f for doubl datatypes since float datatyles eventually convert to double datatypes inside printf.
There is no provision to print float data. Please find the discussion here : Correct format specifier for double in printf
Use this line of code when scanning the second value:
scanf(" %lf", &b);
also replace all %ld with %lf.
It's a problem related with input stream buffer. You can also use fflush(stdin); after the first scanning to clear the input buffer and then the second scanf will work as expected. An alternate way is place a getch(); or getchar(); function after the first scanf line.
Correct ways is:
double a,b;
printf("--------\n"); //seperate lines
scanf("%lf",&a); //%lf is long float numbers
printf("--------\n");
scanf("%lf",&b);
printf("%f %f",a,b); //%f float number
Using %lf will help you in solving this problem.
Use :
scanf("%lf",&doub)
This question already has answers here:
Problems with scanf and doubles [duplicate]
(2 answers)
Closed 8 years ago.
I wrote the following code for an intro to C class. However for some reason I cannot figure out why the scanf will not store the input into the fahrenheit variable therefore not allowing me to do the calculation correctly. I switched the starting value from 0 to 212 to make sure that my calculation is correct however it still doesn't allow me to update.
#include <stdio.h>
int main(void){
double fahrenheit = 212.00;
double celcius = 0;
//prompt the user for the information
printf("Enter a temperature in degrees Fahrenheit >");
//store the information in the Fahrenheit var.
scanf("%f", &fahrenheit);
//calculate the change in metrics
celcius = (fahrenheit-32)*.5556 ;
printf("%f degrees Fahrenheit is equal to %f degrees celcius\n",fahrenheit,celcius);
}
The proper printf and scanf format to use with double argument is %lf. Not %f, but %lf. Don't use %f with double. It should be
scanf("%lf", &fahrenheit);
...
printf("%lf degrees Fahrenheit is equal to %lf degrees celcius\n",
fahrenheit, celcius);
Note that %f will work with double in printf (not in scanf), but using it in that fashion is still a bad habit, which only fuels the popular beginner's misconception that printf and scanf are somehow "inconsistent" in that regard.
The matching between format specifiers and argument types is well-defined and consistent between printf and scanf:
%f is for float
%lf is for double
%Lf is for long double.
You're reading a float (with %f) and yet you're storing it inside a double.
Either
Change the type of fahrenheit to float
Change your scanf call to `scanf("%lf", &fahrenheit);
You declared your variable as double fahrenheit; but used the scanf() specifier for float, try this
#include <stdio.h>
int main(void) {
float fahrenheit = 212.00;
/* ^ float, instead of double */
float celcius = 0;
// prompt the user for the information
printf("Enter a temperature in degrees Fahrenheit > ");
// store the information in the Fahrenheit var.
if (scanf("%f", &fahrenheit) != 1) // check that scanf succeeded
return -1;
// calculate the change in metrics
celcius = (fahrenheit-32)*.5556 ;
printf("%f degrees Fahrenheit is equal to %f degrees celcius\n",fahrenheit,celcius);
return 0;
}
or change the scanf() specifier to "%lf" the printf() specifier is ok for both.
Also, you better make sure scanf() succeeded reading.
I am having problem with floating point numbers. I think something is clashing here.
The output is :
My code :
#include <stdio.h>
int main(){
double y;
printf("enter a decimal number: ");
scanf("%f", &y);
printf("%f\n", y);
fflush(stdin);
getchar();
return 0;
}
I am using Dev C++ 4.9.9.2 and first of all, I have to flush the carriage return out several times if it's not read previously . So if there is any alternative there then tell me. And secondly the output is giving this...
Use
scanf("%lf", &y);
instead. Since scanf("%f", &y); works for floats only.
If you enable compiler warnings it would tell you that the format specifier "%f" expects a float * and not double * argument.
Using wrong format specifier invoke undefined behavior and that's why you are getting unexpected result. Once UB is invoked, you may either get expected or unexpected result. Nothing can be said.
Use %lf to read double type data.
You are using the wrong format specifier in the scanf and doing this will result in UB(Undefined Behaviour).The correct format specifier for a double is %lf while that of a float is %f. So Just change your scanf to
scanf("%lf",&y);
You should use
scanf("%lf", &y);