I am trying to learn C and have come up with the following small program.
#include "stdafx.h"
void main()
{
double height = 0;
double weight = 0;
double bmi = 0;
printf("Please enter your height in metres\n");
scanf_s("%f", &height);
printf("\nPlease enter your weight in kilograms\n");
scanf_s("%f", &weight);
bmi = weight/(height * height);
printf("\nYour Body Mass Index stands at %f\n", bmi);
printf("\n\n");
printf("Thank you for using this small program. Press any key to exit");
getchar();
getchar();
}
The program compiles perfectly, however the answer returned by the program does not make sense. If I enter 1.8 for height and 80 for weight, the bmi is like 1.#NF00 which does not make sense.
What am I doing wrong?
When using scanf with a double, you must use the %lf specifier, as pointers are not promoted with scanf.
For more info, read the following question:
Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
scanf (and scanf_s) format %f expects pointer to type float.
Simply change the type of your height and weight variables to float to fix this.
I think issue in scanf_s syntaxis, you ommited 3-rd argument, which is size of buffer in bytes. Try following:
scanf_s("%lf", &valueToGet, sizeof(double));
the drawback of the scanf() and printf() is that it requires very strict format, any mismatch between the control string and the argument can cause drastic error which makes your input or output make no sense at all. And that mistake is often made by beginners.
If you're using %f format specifier, then you must use float data type instead of double.
The problem is because:
format '%f' expects argument of type 'float*', but argument 2 has type 'double*'
There are two ways to handle this:
Either the variables should be float:
double height = 0; --> float height = 0;
double weight = 0; --> float weight = 0;
double bmi = 0; --> float bmi = 0;
or the format specifier should correspond to double.
scanf_s("%f", &height); --> scanf_s("%lf", &height);
scanf_s("%f", &weight); --> scanf_s("%lf", &weight);
printf("\nYour Body Mass Index stands at %f\n", bmi);
|
V
printf("\nYour Body Mass Index stands at %lf\n", bmi);
Related
#include <stdio.h>
int main(void) {
float base, height, hyp;
printf("input base of triangle:\n");
scanf("%f", base);
printf("input height of triangle:\n");
scanf("%f", height);
printf("input hypotenuse of triangle:\n");
scanf("%f", hyp);
float perimeter = base + height + hyp;
printf("the perimeter of your triangle is: %f\n", perimeter);
return 0;
}
I'm running this through ideone.com and it shows success, then standard input is empty, then in stdout it prints all my print statements with no numbers
This is because ideone is not interactive. Unlike running your program from the command line, ideone requires you to provide all the input upfront in the "input" tab:
You need to enter all your data before running your program.
P.S. Once you do, notice how you have undefined behavior because you pass values, rather than pointers, to scanf. The best way to address this on ideone is to pick "C99 strict" option when compiling your C code. This would break your compile with the following warning:
prog.c:7:11: error: format '%f' expects argument of type 'float *', but argument 2 has type 'double' [-Werror=format=]
scanf("%f", base);
scanf requires a pointer to your data type, you should pass the address of your variables using &:
scanf("%f", &base);
scanf("%f", &height);
scanf("%f", &hyp);
To add, some error checking might be useful, something like:
if(scanf("%f", &base) != 1) //number of items scanned is expected to be 1
//process error..
//etc
Here is my code:
#include <stdio.h>
int main (void)
{
double itemCost;
double paidMoney;
int changeDue;
printf("How much does the item cost: ");
scanf("%lf", itemCost);
printf("How much did the coustomer pay: ");
scanf("%lf", paidMoney);
changeDue = ( (itemCost - paidMoney) * 100);
printf("Change due in pennies is: %i", changeDue);
}
The program will have a simple inputs like 9.5 which represents £9.50 therefore I am using double to store my values. Also printf and scanf promotes floats to doubles so it does not really matter.
However, when compiling with gcc, I get an error message saying:
cashReturn.c:10:15: warning: format specifies type 'double *' but the argument has type 'double' [-Wformat]
What does this error mean and why is it popping up?
You must pass a pointer to a variable of the specified type when using scanf.
double itemCost;
double paidMoney;
int changeDue;
printf("How much does the item cost: ");
scanf("%lf", &itemCost);
// ----------^
printf("How much did the coustomer pay: ");
scanf("%lf", &paidMoney);
// ----------^
Also, you're neglecting to check the return value of scanf. This is not optional! scanf returns the number of items successfully assigned. If it returns N, but you specified M variables to be assigned, then the last (N-M) variables are left unassigned (and in your case uninitialized).
Try something like this:
for (;;) {
printf("How much did the coustomer pay: ");
if (scanf("%lf", &paidMoney) == 1)
break; // success
printf("Invalid input!\n");
}
I am trying to read in a double value continuously from the user using scanf.
Code:
printf("Enter A value: \n");
double input;
int result = scanf("%f", &input);
printf("INPUT: %f\n", input);
The output is
INPUT: 0.000
You lied to the compiler: when scanning, %f says you supply a pointer to float. But you provided a pointer to double.
To fix, either use %lf or declare input as float.
Note that there is an asymmetry with printf formats, which uses %f for both float and double arguments. This works because printf arguments are promoted to double (and are not pointers).
I am trying to read in a double value continuously from the user using scanf.
To do so you need a loop, like the following:
while(scanf("%lf", &input) == 1) {
//code goes here...
printf("INPUT: %lf\n", input);
//code goes here...
}
Note that, as the primitive type of input is double, you need to use %lf instead of %f (%fused for float).
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.
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.