#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
Related
I've had this problem with a couple of programs now, and I can not figure out why this keeps happening. This is my code:
#include <stdio.h>
#include <math.h>
int main(void){
double x = 0;
while(x <= 0){
printf("Enter a digit greater than 0.\n");
scanf("%lf", &x);
}
printf("%lf", &x);
}
and the output is:
Enter a digit greater than 0.
4
0.000000
please help
Use the following code
#include <stdio.h>
#include <math.h>
int main(void){
double x = 0;
while(x <= 0){
printf("Enter a digit greater than 0.\n");
scanf("%lf", &x);
}
printf("%lf", x);
}
You are using
printf("%lf", &x);
But the correct syntax to print value of x is:
printf("%lf", x);
Check the working code https://ideone.com/lpdlmX
First off,
printf ("%lf", &x);
will try to print the address of x rather than its value, and this is clearly into undefined behaviour territory(a). A half-decent compiler should warn you about this such as, with gcc:
program.c: In function 'main':
program.c:13: warning: format '%lf' expects argument of type
'double', but argument 2 has type 'double *' [-Wformat=]
printf ("%lf\n", &x);
^
Secondly, the normal printf specifier for double is %f rather than %lf. You can use the latter since the standard states it has no effect on certain data types but it's a bit of a waste doing so.
So what you need is actually:
printf ("%f", x);
The general rule is that you pass addresses to scanf because it needs to populate the objects at those addresses. For printf, you just pass the object itself (yes, even if the data is a pointer that you want printed as a pointer rather than pointing the object being pointed to).
And, finally, to make your code more robust, you would be wise to detect a problem with scanf since, it there's a problem with the input that leaves x set to zero, the program will continuously try to read that input, resulting in an infinite loop.
So a good starting point, taking all those comments into consideration, would be:
#include <stdio.h>
#include <math.h>
int main (void) {
double x = 0;
while (x <= 0) {
printf ("Enter a digit greater than 0.\n");
if (scanf ("%lf", &x) != 1) {
printf ("Invalid input\n");
return 1;
}
}
printf ("%f\n", x);
return 0;
}
(a) Specifically, from ISO C11 7.21.6.1 /9:
If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
Always always check the return of scanf, that being said, you printf ("%f", x); -- there is no &x and no need for %lf, printf %f format specifiers prints doubles. (however it is required when using scanf -- man scanf and man printf are your friends)
Putting it altogether, you could safely take input as follows:
#include <stdio.h>
int main (void) {
double x = 0;
while(x <= 0){
printf ("Enter a digit greater than 0.\n");
if (scanf ("%lf", &x) != 1) {
fprintf (stderr, "error: invalid input.\n");
return 1;
}
}
printf ("%f", x);
}
note: you must exit the loop on conversion failure (or empty stdin before attempting to take input again -- or your input will fail) Reason: on a failed conversion - no additional characters are read leaving the characters causing the failure just waiting to bite you again on the next go-round...
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've made extremely easy program:
#include <stdio.h>
int main()
{
double x;
printf("Write your number \n");
scanf ("%f", &x);
printf("You've written %f \n", x);
return 0;
}
And as a result strange number appears (no matter what x I give):
"You've written 83096261053132580000000000000000000000000000000000000000000"
What's wrong with this? This program works fine when I change all numbers into an 'int' type.
See what happens when you compile it with warnings enabled:
amb#nimrod-ubuntu:~/so$ gcc -Wall x.c -o x
x.c: In function ‘main’:
x.c:6:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double *’ [-Wformat]
Change %f to %lf and the scanf and printf functions will correctly take a double.
Wrong specifier for scanf().
With scanf(), "%f" matches a float *, yet coded passed a double *. Use "%lf" instead. Code's printf() is fine. See Correct format specifier for double in printf
double x;
printf("Write your number \n");
// scanf ("%f", &x);
scanf ("%lf", &x);
printf("You've written %f \n", x);
A good compiler should have warned of your incorrect code as suggested by #abligh. Either enable all warnings or consider a new compiler.
When scanning, the &x must match the proper scanf() print specifier.
"%f" matches a float *
"%lf" matches a double *
The using printf() things are easier. If a float or double is passed, being a variadic function, float is promoted to double.
"%f" and "%lf" match a double
%f is used for float type. You should use %lf for double (Long Float).
You need to use %lf for scanf and printf.
In other words, this:
#include <stdio.h>
int main()
{
double x;
printf("Write your number \n");
scanf ("%lf", &x);
printf("You've written %lf \n", x);
return 0;
}
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);
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.