0

I am new to stackoverflow so sorry for my inappropriately long question or format of question.

I tried this program to calculate gravitational force.so far i have tried these things and they don't seem to work.

#include <stdio.h>
#include <conio.h>
#include <math.h>
#define G 6.67e-11


main()

{

 long double r;

 long double m1,m2,F;
printf("Enter m1:-");
scanf("%Lf",&m1);
printf("Enter m2:-");
scanf("%Lf",&m2);
printf("Enter distance between m1 and m2:-");
scanf("%Lf",&r);
F=(G*m1*m2)/(r*r);
printf("The force is:-%Le",F);

return; 

}

Now i have tried many variations in the last line of code.

printf("The force is:-%e",F);

like instead of %e i have tried %Lf as i have defined F as long double. also when i run the code as is above and enter the following inputs

ie.

Enter m1:10

Enter m2:2

Enter m3:10

answer as

 force is:1.327e-317

instead of 1.334e-11.

which i calculated in calculator.

and if there is a problem with #define please elaborate about how i can define constants in exponential form and use them for similar calculations.

please help.

Samarth
  • 3
  • 5

2 Answers2

2

scanf takes the address of the variable to be able to write into it but printf takes the value as a parameter. Since printf is a variable arguments function, it is unable to check parameters type so you have to pass the proper types.

do that instead:

printf("The force is:-%e",F);

I have followed the link and adding that switch works for me on windows:

-D__USE_MINGW_ANSI_STDIO

The force is:-1.334000e-011
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • yes thank you i removed the ampersand sign. and ran the code with double as data type. and %e in last printf but the output in force is #QNAN0e+000 – Samarth Jul 12 '16 at 17:45
0

You're using the wrong format specifier to print the result.

F is of type long double, but %e is used for double. You need to use %Le.

Using the wrong format specifier results in undefined behavior.

Also, you need to pass in F, not &F for printf.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Sir i just tried as you suggested but unfortunately the ans still comes out to be 1.327e-317. – Samarth Jul 12 '16 at 17:40
  • @Samarth Did you fix the format specifier *and* remove the `&` before `F`? – dbush Jul 12 '16 at 17:41
  • yes i did that. and also as you said i changed the data type to double and kept the %e and ran the program. but the output is #QNAN0e+000 – Samarth Jul 12 '16 at 17:47
  • @Samarth No, don't change the data type. Leave it as `long double` and change `%e` in the `printf` to `%Le`. – dbush Jul 12 '16 at 17:49
  • Yes sir i did that and the output is -2.000000e+000. – Samarth Jul 12 '16 at 17:51
  • @Samarth I have changed your program according to dbush's instructions. It produces the correct/expected results. In short, make _no_ other changes other than the `printf` should be: `printf("The force is:-%Le", F);` – Craig Estey Jul 12 '16 at 17:55
  • 1
    owww possibly MinGW related problem: http://stackoverflow.com/questions/26296058/cant-print-correctly-a-long-double-in-c – Jean-François Fabre Jul 12 '16 at 17:55
  • @Jean-François Fabre yes i checked out the link you provided i think it might be the case.i use codelite on windows with mingw,any better alternatives to that are welcome. – Samarth Jul 12 '16 at 18:01
  • I have edited the answer. it works (tested here) – Jean-François Fabre Jul 12 '16 at 19:11