Wrong values when reading floats from a text file in c [duplicate] - c

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I am reading text file contains values like below
125602365 119653955 126374444 124463807 127312438 128395899
and the below code to read it.
if(!(pvtcheck(0)))
{
fscanf(fp,"%f",&deltime);
printf("\ndeltime0=%f\n",deltime); ///Actual value is 125602365, but i am getting 125602368.
}
if(!(pvtcheck(1)))
{
fscanf(fp,"\t%f",&deltime);
printf("\ndeltime1=%f\n",deltime);///Actual value is 119653955, but i am getting 119653952.
}
/// same for pvtcheck(2),pvtcheck(3),pvtcheck(4)
if(!(pvtcheck(5)))
{
fscanf(fp,"\t%f",&deltime);
printf("\ndeltime5=%f\n",deltime); ///Actual value is 128395899, but i am getting 128395896.
}
In the Comment i have written output , any solution to not change the values

You need to use the double type instead of float, and the %lf format specifier for scanf instead of %f.
double deltime;
fscanf(fp, "%lf", &deltime);
printf("\ndeltime0=%lf\n", deltime);
fscanf(fp, "\t%lf", &deltime);
printf("\ndeltime1=%f\n", deltime);
...
BTW: for printf you can use either %f or %lf they both have the same meaning.
Also read this SO article

Related

how to convert float to string correctly in avr c [duplicate]

This question already has answers here:
snprintf not working as expected with avr-gcc
(2 answers)
Problems with printf() on AVR in C with floating-point
(3 answers)
Closed 10 months ago.
I need to convert a float value to a string in my avr project. Therefore I tried it with the following code:
char buf[10];
float emg1 = 33.42;
sprintf(buf, "%f", emg1);
uart_puts(buf);
But the output is only a "?". I tried to change the char format from %f to %g but I only got "?".
Is there another way to simply convert float to string, or can somebody tell me where the mistake is?

Why isn't the output a float when the format specifier is %f? [duplicate]

This question already has answers here:
What happens when I use the wrong format specifier?
(2 answers)
Closed 1 year ago.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a=100;
printf("%f",a);
return 0;
}
As we can see I declared a variable a with integer data type, so that it's going to allocate 4 bytes of space.
Next in printf I have used %f ( float) that means it have to print in float value whatever the input is, right?
But when I execute the code i'm getting 0.000000 as an output (whatever value I give it's just giving the output 0.0000).
Try this:
printf("%f", (double)a);
The reason it did not work was wrong formatting syntax. You can change the whole syntax, or rather use the casting option above.
Note: you can include any int value in float range, but cannot do otherwise.

How to get answers of calculations in form of exponent(i.e-6.6.7e-11) in C language [duplicate]

This question already has an answer here:
can't print correctly a long double in C
(1 answer)
Closed 6 years ago.
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.
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
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.

why formatting input output requires variable to be supplied? [duplicate]

This question already has answers here:
Turbo C++: Why does printf print expected values, when no variables are passed to it?
(5 answers)
Closed 9 years ago.
I have just tried to know the output without supplying the variable instead just %d and there is no error in compiling the program but i wonder how the output displayed like below.
#include <stdio.h>
int main()
{
printf("%d");
return 0;
}
The output became 7288368
"why formatting input output requires variable to be supplied?"
Because the implementation of printf demands so. From the manual page of printf:
"Each conversion specification is introduced by the character %, and ends with a conversion specifier... The arguments must correspond properly (after type promotion) with the conversion specifier."
You have used "%d" format string, which expects a integral argument appropriate for decimal conversion, yet you haven't provided any arguments, which resulted in an undefined behavior

"formatted" C language [duplicate]

This question already has answers here:
Formatted and unformatted input and output and streams
(3 answers)
Closed 9 years ago.
I want to know what is meant by formatted in the printf and scanf functions of the C language. I am new to C programming and did not understand what is meant by formatted. Can anyone please give me the appropriate explanation?
printf("%s %d", "abc", 123);
%s %d is the format string and contains instructions for what to do with the arguments. In this case, it says print string, print a space, then print an integer.
As in formatted output
So you can 'print' say a double called myMoney of 11003.145 as $11,003.15.

Resources