pow type problem

B

Bill Cunningham

Is this one of those rare instances where casts are needed? I have this
code and the compiler complains that the prototypes are wrong.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
main (int argc, char *argv[])
{
if (argc != 4)
{
puts ("usage error");
exit(EXIT_FAILURE);
}
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", pow (argv[1], argv[2]));
return 0;
}

And I did try to link with libm.a

Bill
 
B

Bill Cunningham

Ok I see now. Sorry false alarm. I see a couple of errors in the code.
D?mn. The pow needs xand y Sorry again.

Bill
 
W

Walter Roberson

Is this one of those rare instances where casts are needed? I have this
code and the compiler complains that the prototypes are wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
main (int argc, char *argv[])
{
if (argc != 4)
{
puts ("usage error");
exit(EXIT_FAILURE);
}
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", pow (argv[1], argv[2]));

It would seem to me to make more sense to use

printf ("%.2f\n", pow (x, y));
return 0;
}
And I did try to link with libm.a


Question: what are you expecting in argv[3] ? You exit the program
if argc != 4 but you do not make use of the 4th argument, just
the 2nd and 3rd (it being quite understandable why you aren't making
use of the 1st argument.)
 
K

Keith Thompson

Bill Cunningham said:
Is this one of those rare instances where casts are needed? I have this
code and the compiler complains that the prototypes are wrong.

No, no casts are needed.

Presumably your compiler is complaining that the call is wrong, not
that the prototype is wrong.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
main (int argc, char *argv[])
{
if (argc != 4)
{
puts ("usage error");
exit(EXIT_FAILURE);
}

You use only the first two arguments. Why do you want argc to be 4
rather than 3?
double x, y;
x = strtod (argv[1], NULL);
y = strtod (argv[2], NULL);
printf ("%.2f\n", pow (argv[1], argv[2]));

argv[1] and argv[2] are of type char*. pow expects two arguments of
type double.

You've just declared and assigned values to two objects x and y of
type double, but you never use them.

Think about it.
 
B

Bill Cunningham

Question: what are you expecting in argv[3] ? You exit the program
if argc != 4 but you do not make use of the 4th argument, just
the 2nd and 3rd (it being quite understandable why you aren't making
use of the 1st argument.)

4 was one of the bugs. I didn't see it until after I posted. Sorry.

Bill
 
B

Bill Cunningham

argv[1] and argv[2] are of type char*. pow expects two arguments of
type double.

You've just declared and assigned values to two objects x and y of
type double, but you never use them.

Think about it.
return 0;
}

And I did try to link with libm.a
Thanks.

Bill
 
N

Nick Keighley

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int
main (int argc, char *argv[])
{
  if (argc != 4)
    {
      puts ("usage error");
      exit(EXIT_FAILURE);
    }
  double x, y;

you can't mix statements and declarations in C90.
For C90 compilance put the double x, y;
before the if

  x = strtod (argv[1], NULL);
  y = strtod (argv[2], NULL);
  printf ("%.2f\n", pow (argv[1], argv[2]));
  return 0;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

completely stuck 14
Where is my mistake? Why is s equal to minus infinity at some loop iterations? 0
sh?tpile of errors 82
z error 12
Function is not worked in C 2
Command Line Arguments 0
code question 74
root(s) 6

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top