Reading in double values with scanf in c

CScanfConversion Specifier

C Problem Overview


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.

C Solutions


Solution 1 - C

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);

Solution 2 - C

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);

Solution 3 - C

You are using wrong formatting sequence for double, you should use %lf instead of %ld:

double a;
scanf("%lf",&a);

Solution 4 - C

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 : https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf

Solution 5 - C

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.

Solution 6 - C

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

Solution 7 - C

Using %lf will help you in solving this problem.

Use :

scanf("%lf",&doub)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1880009View Question on Stackoverflow
Solution 1 - CsimoncView Answer on Stackoverflow
Solution 2 - CKyborekView Answer on Stackoverflow
Solution 3 - CpiokucView Answer on Stackoverflow
Solution 4 - CBiswajit RoyView Answer on Stackoverflow
Solution 5 - CJameSBonDView Answer on Stackoverflow
Solution 6 - CCristi G.View Answer on Stackoverflow
Solution 7 - CAkshansh View Answer on Stackoverflow