0
#include <stdio.h> //function prototype double triangle(double b ,double h); //main function int main(void) { //declare variables double height,base, ans; //take input printf("Please give the height of the triangle:"); scanf("%d",&height); printf("Please give the base of the triangle:"); scanf("%d",&base); //pass variables to triangle function and equal returned value to ans ans = triangle(base,height); //prdouble returned doubleeger system("CLS"); system("COLOR C"); printf("\n\n\t\t\tThe area of the triangle is: %d\n\n\n", ans ); return 0; } //area function double triangle(double b, double h) { printf("Hello"); //declare varible double a; //process data a = (b*h)/2; //return ans to main return (a); } 

Above is the code for a program to compute the area of a triangle, but while it works when I use integers, when I try to use doubles it fails to run the "triangle" function. Could I get some advice on why this is?

11
  • 1
    How does it fail to run a function? What are the characteristics of this failure? Commented Nov 16, 2015 at 22:53
  • 3
    You need to use %lf instead of %d format to read into double. Using %d indicates to scanf() that the pointer is an int *; using %lf indicates it is a double *. Also remember to print your inputs for checking; this would have told you a lot about what's going wrong. And if your compiler wasn't warning you about the mismatch between the format string and the pointers, it is time to turn on the warnings, or get a better compiler. Commented Nov 16, 2015 at 22:54
  • 2
    @BobbySacamano: for printf, yes - floats and doubles are treated the same. But for scanf, no - you need %f for float and %lf for double. Commented Nov 16, 2015 at 22:57
  • 1
    @BobbySacamano: it is a significant difference between printf() and scanf() — a source of endless problems. With printf(), the %f format serves for both double and float because the compiler automatically converts float to double in the call. You can now use %lf for double in printf(), mainly for symmetry with scanf(). In scanf(), %f indicates a float * argument; %lf indicates a double * argument. You use %Lf in both printf() and scanf() to indicate long double (but the types are long double and long double *, of course). Commented Nov 16, 2015 at 23:00
  • 2
    %i accepts 0xABCD as a hex and 007 as octal and 1234 as decimal; %d only accepts decimal (and %x only accepts hex and doesn't require the 0x prefix, and %o only accepts octal and doesn't require the 0 prefix). Commented Nov 16, 2015 at 23:02

5 Answers 5

2

Use %lf to read double numbers:

scanf("%lf",&height); scanf("%lf",&base); 

Use %f to print double or float variables:

printf("\n\n\t\t\tThe area of the triangle is: %f\n\n\n", ans ); 
Sign up to request clarification or add additional context in comments.

Comments

2

%d as format string of scanf is used for int values. For float values you need %f (or %lf for doubles).

A mismatch between format string and pointer type is "undefined behavior" and can lead to all kind of errors (or even just work in some cases).

Comments

1

%d is actually used for integer despite looking like it might represent double (I like to think of %d as digit) .

%f is used for floating point numbers.

%lf is used for double type. double is a type of float with double the number of bits. double usually has 64 bits and float is usually 32 bits. The l is a length modifier for the larger type. (I like to remember it as 'long float'.)

1 Comment

What you've actually said is correct, but the way you said it has the potential to mislead. If you said While it might seem plausible that %d would be used for double, it is … that would have less chance of misleading. That is somewhat a matter of emphasis. I'd go with a more direct Despite anything you may think to the contrary, the %d format specifier is used for decimal integers (with %x for hexadecimal and %o for octal) and %lf is used for double values.
0

Without knowing what "fails" means in your question I would try and change your scanf statements to use the double format specifier %lf rather than %d.

scanf("%lf", &height); 

Comments

0

as others have said, you need to change the type that you neeed to take from the user.

scanf("%lf %lf", &height, &base); 

this should do the trick. good luck M8.

P.S "%lf" and "%f", both give you the same result. six digits of precision after the decimal point.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.