2

Keeps saying that there is an expected expression before double int pt function

 #include <stdio.h>
 int pt (int a, int b)
 {
    int c, result;
      c = (a * a) + (b + b);
        result = double sqrt (double c);
   return result;
 }
 int main (void)
 {
     int d, e, f;
     int pt (int a, int b);
     printf("type enter after input of the two legs");
     scanf("%i", &d);
     scanf("%i", &e);
     f = pt (d,e);
     printf("the hypotenuse is %i", f);
     return 0;
 }
2
  • 1
    Others have already explained why your program doesn't compile as written, but I wonder: why did you write pt() to return an int in the first place? The hypotenuse of a right triangle whose sides are of integer length is usually not itself an integer. Also, you want (b * b) rather than (b + b). Jul 16, 2014 at 1:47
  • Because this is practice Jul 19, 2014 at 19:53

3 Answers 3

7

Change

result = double sqrt (double c);

to

result = sqrt(c);

You don't have to cast c into a double when you pass it into the sqrt function because of implicit conversion. If you still wanted to do the cast, the correct way would be sqrt((double) c).

Also, #include <math.h> for use of the sqrt function.


Note: It's not required to cast the return type of sqrt to int (relevant since result is of type int) - however some compilers may give warnings about implicit conversion (Credit to @MattMcNabb). It can also be good practice to put the cast in to signal to other coders that the precision loss is intentional.

2
  • 3
    A cast is not required; result = sqrt(c); is correct code. There is implicit conversion both ways between double and int. The only effect of using a cast here is as a hack to disable a non-standard warning that some compilers issue.
    – M.M
    Jul 16, 2014 at 1:50
  • @MattMcNabb Thank you, didn't know about the two-way implicit conversion.
    – Andrew_CS
    Jul 16, 2014 at 2:01
2

double sqrt (double c); is a function declaration. (It is also a function prototype). This is how you announce that a function exists, and what parameters it takes and what it returns. The word c here does not mean anything, it can be omitted.

When you want to call a function you do not repeat this info. You just give the function name, followed by a list of values, e.g. sqrt(c) .

In your code , sqrt has not been declared. Functions must be declared before they are called. It's not possible to simultaneously declare a function and call it; you have to declare it first.

(Historical note: this is true from C99 onwards; in C89 you could call an undeclared function, and the compiler would assume you wanted the function declared to return int ; this would cause undefined behaviour for any function that does not actually return int).

It is possible for you to provide your own declaration, so long as it matches the standard declaration:

double sqrt(double);

However it is a better idea to just use the standard declaration by going:

#include <math.h>

which works because the file math.h includes the line double sqrt(double); (or something equivalent).

Then you can write:

result = sqrt(c);

Note that you do not need to use any casts in relation to this function call. Since a function prototype exists, the compiler knows that even if you supply an int, it should convert that int to a double (which it knows how to do), and vice versa.

2
  • Just for the record: “it's not permitted for you to write your own declaration”—yes, it is. The standard allows to write double sqrt(double); instead of the #include. Of course, that's not really useful or anything, but “it's not permitted” is wrong.
    – mafso
    Jul 16, 2014 at 2:25
  • @mafso thanks, fixed. Technically, you should do #undef sqrt before the self-declaration in case stdio.h included math.h, and math.h defined a macro version.
    – M.M
    Jul 16, 2014 at 2:33
0

It is talking about the casting that you are doing? your casting is complaining it should be like this:

your result is an integer, so you should be casting it to int.. but if you want to cast anything to double it should be as follow

result = (double)sqrt((double) c) or another way is double(sqrt((double) c)

but if you want to cast it as int then result = (int)sqrt((double) c) or result = int(sqrt((double) c)) hope this helps good luck

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.