I just asked a question but couldn't get what I want, and couldn't edited and reply as it is too long. My question is the same and it is the link to that question.how to keep decimals in c
#include<stdio.h>
int main()
{
float b,c,;
int a,kk;
printf("Welcome to the unit conversion program\n");
printf("This program can convert the measurements of;\n");
printf("1-Length\n");
printf("2-Mass\n");
printf("Please select the number that corresponds to the measurement you want to convert in:\n");
scanf("%d",&a);
if (a==1){
printf("Your number will be converted from inches to centimeters.\n");
printf("Please enter the length.\n");
scanf("%f",&b);
c=b*2.54;
printf("%f inch is %f cm.",b,c);
scanf("%d",kk); \. to avoid to shut the cmd windows .\
}
else if (a==2){
printf("Your number will be converted from pounds (lbs) to kilograms");
printf("Please enter the mass.\n");
scanf("%d",&b);
c=b*0.45359237;
printf("%d lbs is %d kgs.",b,c);
}
return 0;
}
The error is that %d in the format specifier of both scanf and printf represents decimal, so it expects an int (in the case of scanf, a pointer to int)
Since you are declaring b and c as float, %d in the lines
scanf("%d",&b);
printf("%d lbs is %d kgs.",b,c);
should be changed to %f respectively.
A little piece of advice: use double in stead of float. It provides more precision, its performance is better in many machines. The downside is that it occupies more space than float, but that's not a problem in most cases.
You should use %f in printf and %lf in scanf in case you are using double.
Change:
scanf("%d",&b);
printf("%d lbs is %d kgs.",b,c);
to:
scanf("%f",&b);
printf("%f lbs is %f kgs.",b,c);
You declared b as float and taking input as a decimal & later printing both float b,c as decimal? Please study a book Deital & Deital for C/C++ is an amazing book.
Related
I am trying to write a sort of calculator program and i can get everything but my factorial and power functions to work. They output a number in the millions no matter how small the number is and i don't see a problem with the code. (I just started learning C recently so assume the extent of my knowledge is everything in this code)
int iFactorial(num1){//needs help returns a number in the millions no matter what
int i, factorial=1;
printf("Enter a positive number: ");
scanf("%d", &num1);
for(i=1; i<=num1; i++)
factorial*=i;
printf("The factorial is %i", &factorial);
return 0;
}
int fPower(num1,num2){//needs help, same as above
int i, number = 1;
printf("Enter the number you want to raise to a power: \n");
scanf("%d", &num1);
printf("Enter the exponent: ");
scanf("%d", &num2);
for(i=0; i<num2; i++)
number*=num1;
printf("%d to the %d equals %d", &num1, &num2, &number);
return 0;
}
You are using & in the print statement that prints the address of the variable used.
Correct the statements in their respective function as follows :
printf("The factorial is %i", factorial);
printf("%d to the %d equals %d", num1, num2, number);
Your printf for the factorial and power cases are mal-formed, you are passing the arguments by pointer; you need to pass them by value.
After that, you'll realise quickly that you'll overflow the int type in the factorial and power cases. An int in general is only good up to and including 7! in truly portable C++. Consider using an unsigned long long, which will give you values up to and including 21!. Use "%ull" for an unsigned long long in the formatter.
Finally, pass the types explicitly to your functions in C: your style has been explicitly disallowed since C99.
You're using the wrong format specifiers to printf to print the results, and you're not passing the actual values you want to print:
printf("The factorial is %i", &factorial);
You're passing in the address of factorial instead of its value, so it's printing that instead. Just pass the result directly instead of its address:
printf("The factorial is %i", factorial);
This question already has answers here:
Problems with scanf and doubles [duplicate]
(2 answers)
Closed 8 years ago.
I wrote the following code for an intro to C class. However for some reason I cannot figure out why the scanf will not store the input into the fahrenheit variable therefore not allowing me to do the calculation correctly. I switched the starting value from 0 to 212 to make sure that my calculation is correct however it still doesn't allow me to update.
#include <stdio.h>
int main(void){
double fahrenheit = 212.00;
double celcius = 0;
//prompt the user for the information
printf("Enter a temperature in degrees Fahrenheit >");
//store the information in the Fahrenheit var.
scanf("%f", &fahrenheit);
//calculate the change in metrics
celcius = (fahrenheit-32)*.5556 ;
printf("%f degrees Fahrenheit is equal to %f degrees celcius\n",fahrenheit,celcius);
}
The proper printf and scanf format to use with double argument is %lf. Not %f, but %lf. Don't use %f with double. It should be
scanf("%lf", &fahrenheit);
...
printf("%lf degrees Fahrenheit is equal to %lf degrees celcius\n",
fahrenheit, celcius);
Note that %f will work with double in printf (not in scanf), but using it in that fashion is still a bad habit, which only fuels the popular beginner's misconception that printf and scanf are somehow "inconsistent" in that regard.
The matching between format specifiers and argument types is well-defined and consistent between printf and scanf:
%f is for float
%lf is for double
%Lf is for long double.
You're reading a float (with %f) and yet you're storing it inside a double.
Either
Change the type of fahrenheit to float
Change your scanf call to `scanf("%lf", &fahrenheit);
You declared your variable as double fahrenheit; but used the scanf() specifier for float, try this
#include <stdio.h>
int main(void) {
float fahrenheit = 212.00;
/* ^ float, instead of double */
float celcius = 0;
// prompt the user for the information
printf("Enter a temperature in degrees Fahrenheit > ");
// store the information in the Fahrenheit var.
if (scanf("%f", &fahrenheit) != 1) // check that scanf succeeded
return -1;
// calculate the change in metrics
celcius = (fahrenheit-32)*.5556 ;
printf("%f degrees Fahrenheit is equal to %f degrees celcius\n",fahrenheit,celcius);
return 0;
}
or change the scanf() specifier to "%lf" the printf() specifier is ok for both.
Also, you better make sure scanf() succeeded reading.
At the momemt, i'm learning the C and the basics of the language, but I have a problem with my code. When I multiply two numbers, I cant get the decimals, even I float the numbers I enter.
My code:
int main()
{
double result_met, km;
result_met = km = 0.0f;
/*Display text*/
printf("Enter values of the distance between the two cities in km's\n");
scanf_s("%d", &km);
/*Formular for a simple km conversion*/
result_met = km * 1000.0f;
/*Result print*/
printf("Meter:%d", result_met);
printf("\nWaiting for a character to be pressed from the keyboard to exit.\n");
getch();
return 0;
}
The format specifiers are incorrect - it should be %lf - %d is for int.
scanf_s("%lf", &km);
/*Formular for a simple km conversion*/
result_met = km * 1000.0f;
/*Result print*/
printf("Meter:%lf", result_met);
Format string specifications
The format specifier for double is %lf, not %d
You may as well use a float instead of a double, it saves memory (which is important when you begin to write big programs), then you must use the format specifier %f (the "l" in %lf is for "long", because a double is a long float)
When treating decimals, you want to print only a few decimals on the screen (to avoid the printing of a "2.50000001"), then you can use the format specifier %.3f if you want 3 and only 3 digits printed (3 can be any integer).
for example, the following code :
printf("%.2f\n",3.1415);
has the following output :
3.14
the printf function has many different format specifier, that can be very useful.
Go see the cpluplus.com reference if you want to learn more about it
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
I just started to learn C programming.
In my book there is this piece of code:
/*Code Start*/
/*This code is use to find the simple interest*/
main ()
{
int p, n;
float r, si;
p = 1000;
n = 3;
r = 8.5;
si= p*n*r/100;
printf("%f", si);
}
/*Code end*/
The output i got was " 255.000000 "
I though i'll modify it with scanf function so i wrote this:
/*Code Start*/
main ()
{
int p, n;
float r, si;
printf("Enter value for p: \n");
scanf("%d", &p);
printf("Enter value for n: \n\n");
scanf("%d", &n);
printf("Enter valuse for r: \n\n");
scanf("%d", &r);
si= p*n*r/100;
printf("\nYour Simple Interest is %f\n\n", si);
}
/*Code End*/
No matter what values i give to p,n,r the answer i get is always 0.000000..
I also tried giving the values, p=1000, n=3, r=8.5 but still i get 0.000000..
Change the specifier in scanf. You're using %d instead of %f:
scanf("%f", &r);
^
First side note: the code looks kind of bad (no return type for main ?!). Are you sure it's a good book ?
Second side note: using floats today is kind of pointless. Maybe you
should use doubles ?
Firstly, your main problem: The %d specifier is only for integers, not floats or doubles. Use %f for floats.
In addition, the main should return an int, this will do:
int main() {
/* your code */
return 0;
}
Finally, I would recommend you make better use of white-space as it will vastly help with readability once you start making larger programs.
Use %f conversion specification to read a float:
scanf("%f", &r);
%d means it reads a decimal integer and not a float.
r is a float, but you're reading it in using %d as a scanf specifier, which expects an int.
The real culprit in your code is the line scanf("**%d**", &r).
%d is the format specifier for integer value, as you declared r as float then use %f instead of %d.
i.e. scanf("%f", &r)
Change Either
int p, n;
float r, si;
to
int p, n,r;
float si;
or change formate specifier in scanf("%d", &r); %d to %f.
when you declare r as an integer r=8 will be considered, in that case scanf("%d", &r); will be accepted. and your program get compiled and executed.
both declaration and formate specifier should be same.
my suggestion is to use %.2f when dealing with money. which will give like 10.00 which is the correct formate.