Double and Float format displaying different results - c

I thought that the difference between double and float were the precision of the decimals. However, I am getting strange results with using double and float and they are no where close to one another.
The first segment of code is used with the float format producing the correct results:
#include <stdio.h>
#define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) )
int main (void)
{
float number, absNumber;
printf ("What number do you want to check the absolute value for? : ");
scanf ("%f", &number);
absNumber = ABSOLUTE_VALUE(number);
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
return 0;
}
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -3.00 is 3.00
The second segment of code is used with the double format producing incorrect results:
#include <stdio.h>
#define ABSOLUTE_VALUE(number) ( ((number) < 0) ? -(number) : (number) )
int main (void)
{
double number, absNumber;
printf ("What number do you want to check the absolute value for? : ");
scanf ("%d", &number);
absNumber = ABSOLUTE_VALUE(number);
printf ("The absolute value of %.2d is %.2d\n", number, absNumber);
return 0;
}
Output:
What number do you want to check the absolute value for? : -3
The absolute value of -03 is 2147344384

In the second example, %d is used for integers. (d isn't short for double, but for decimal)
scanf ("%d", &number);
should be
scanf ("%lf", &number);
The printf is incorrect too, as %fis used for double
printf ("The absolute value of %.2d is %.2d\n", number, absNumber);
Instead, this should be:
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
Notice that the format specifier for double is different for scanf and printf, this C FAQ article has some good explanations.

Your last printf format string is wrong. Should be.
printf ("The absolute value of %.2f is %.2f\n", number, absNumber);
and if you enable all compiler warnings (e.g. compile with gcc -Wall -g) you'll have a warning about that. Always enable all warnings in the compiler.
Your scanf are also wrong, should be scanf("%lf", &number); and with all warnings the compiler would have warned you.
BTW, scanf returns the "number of successfully matched items" and you should test its result.
You really need to carefully read the documentation of printf(3) and of scanf(3). (In a linux terminal, you could type man 3 printf to get it).
Notice that arguments are converted to double and the conversion specifier for double-s is %f in printf and %lf in scanf
And you should learn how to use a debugger (like gdb).

In your second program, you have used %d format specifier for double which is wrong. And that's why you are getting error. You should use %lf (long float or better called 'double') for printing it.
For other format specifier,
I think this might help you
a link
You can also search different format specifiers on google by typing 'format specifiers'.

Make 2 changes for using double data-type in c :
use %lf in scanf
use %f in printf
//remember : %d is for int
%f is for float
%c is for char

If you read the manual, "%d" is for integers. You need "%lf" instead.
Ditto with the printf

Related

Calculating percentages in C

I am trying to find out how percentages are calculated within C, and specifically I am trying to find out how much taxes you get from user input, if the tax would be 21%.
But after trying the code in the terminal with input : 130,
which means I want 21% of 130$, it gives me a negative value of -858993459
How do Ifix this or where did I go wrong?
All of my google searches come up empty too, but probably because I am not using the correct phrasing so all info is welcome.
#include <stdio.h>
double get_tax_amount(double price_including_taxes)
{
return price_including_taxes *0.21;
}
int main()
{
printf("What was your price? ");
double price;
scanf("%d", &price);
printf("The tax price is: %d", get_tax_amount (price));
}
You’re using the wrong conversion specifier in both scanf and printf. To read a double value with scanf you need to use %lf and to print a double value with printf you need to use %f.
Because of how those functions work, they don’t know the number, type, or order of arguments you’ve actually passed to them - they just see a sludge of bytes on the stack or a sea of argument registers. The only way for them to know what arguments they should expect is through what you specify in the format string.
%d is used to read and print int types, %c for individual characters, %s for strings, etc.
Try "%lf" instead of "%d". "%d" is expecting an integer, which conflicts with variable type "double".
The variable type double should use %lf in scanf function, and %f in printf function.

How to read floating points from terminal? scanf always gets 0.0 [duplicate]

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)

Issues with printing a floating point number from user input in C [duplicate]

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)

Double, Integer, Long double

I apologies in advance if there is a stupid error in this code, but I can't seem to trouble shoot it. My problem is this, I compile with GCC-8 (installed on Mac via home-brew), then execute in the terminal. When using int do define variables s & a, I get zeros as output using the print statements below. If I declare s & a variables as double I still get zeros for the first two print statements, and 1024 for the last print statement. I'm just lost as to what is going on. Appreciate any help!
/* square code */
#include <stdio.h>
int main() {
int s, a;
printf("enter the length of your square \n");
scanf("%f", &s);
a= s * s;
printf("the area of your square is %f cm using f placeholder \n", a);
printf("the area of your square is %lf cm uning fl placeholder\n", a);
printf("the area of your square is %d cm using d placeholder \n", a);
return(0);
}
int s;
scanf("%f", &s);
If s is an int then you need to scan it in like an int using "%d" instead of "%f". What you are doing is undefined behavior, both in scanf() and printf(). An integer needs to be scanned and printed as an integer.
I think the misunderstanding is that you treat the conversion specifiers %d and %f as defining only the output formatting, regardless of the type of value passed.
Actually each conversion specifier is tightly coupled with a particular type of argument it will accept; For example, %d is defined for integral types only, and all other types like floating point or string used together with %d will yield undefined behaviour (cf, for example, cpp-reference of printf for a more detailed explanation).
So double f=1.2;printf("%d",f) will not format a floating point value 1.2 as integral (someone might expect 1 being the output); It rather yields undefined behaviour, having any output (even no output), a crash, a... whatever.
Same applies to scanf-format specifiers, of course.
So if data type is int, use %d; if data type is double, use %f.
In your code you use s as an int, and when you use scanf you are using %f instead of %d. That's the reason why you are getting the first two outputs as a zero.
As for the third output, you will get the expected value, because in that case you are correctly using %d. See the code below:
int s;
printf("enter the value");
scanf("%d", &s);
printf("the area of square is=", s*s);
Hey guys thanks so much for the help. Before checking back this morning I tried one more thing, and alas it was a stupid error.
When declaring "double s,a", I realized my scanf() function was using scanf("%f",s) as opposed to the correct scanf("%lf", s)".
Also thanks for the help for use of %d when using "int s,a".

Scanf for double not working in Dev C++

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

Resources