65

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.

alk
  • 69,737
  • 10
  • 105
  • 255
user1880009
  • 663
  • 1
  • 5
  • 5

7 Answers7

130

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);
simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    tried it already, does only work for the first value, second is still wrong. – user1880009 Dec 05 '12 at 20:12
  • 4
    Much as I love Wikipedia, a decent basic C book might be an even better recommendation for the OP (+1) – NPE Dec 05 '12 at 20:14
  • @NPE Fair point. I thought a recommendation to "read a good book" might be viewed as a bit vague though and the wikipedia page was, surprisingly, the best reference I could find. – simonc Dec 05 '12 at 20:32
  • 1
    @user1880009 You were still using the `%d` format specifier to print the results. This resulted in only 4 bytes of your double being read and interpreted as an int. You need to use the `%lf` format specifier here too. I've updated my answer to show this. – simonc Dec 05 '12 at 20:42
  • ty, this works, at least for the first 2 values. i tried it with three, the first two are ok but the third is wrong, though using ur solution. – user1880009 Dec 05 '12 at 21:00
  • ah, my bad, misstyped, now working for everything, ty 4 help :) – user1880009 Dec 05 '12 at 21:04
  • 3
    `%lf` to print a double was added in C99; if you want to be compatible with older versions of C then `%f` can be used which prints both `float` and `double`. – M.M Nov 11 '14 at 02:57
  • 2
    Tell yourself: `%lf` = **L**ong **F**loat = `double`. – iBug Oct 07 '17 at 13:51
7

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);
Kyborek
  • 1,519
  • 11
  • 20
3

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

double a;
scanf("%lf",&a);
piokuc
  • 25,594
  • 11
  • 72
  • 102
1

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

Biswajit Roy
  • 508
  • 2
  • 7
  • 19
  • While this is true, the question is about scanf, which has historically behaved differently than printf, which didn't support %lf until C99. Unlike scanf, which the question is about. – Lundin Nov 26 '18 at 13:52
0

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.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
JameSBonD
  • 1
  • 1
0

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
Cristi G.
  • 1
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 27 '22 at 09:58
  • all description is in code " //%lf is long float numbers " – Cristi G. May 29 '22 at 12:46
-5

Using %lf will help you in solving this problem.

Use :

scanf("%lf",&doub)
Tharif
  • 13,794
  • 9
  • 55
  • 77
Akshansh
  • 19
  • 3
  • What's new/different in this answer? The same has been posted 4 years before this. – P.P Jul 30 '20 at 07:23