PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > pow type problem
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
pow type problem

Réponse
 
LinkBack Outils de la discussion
Vieux 27/05/2008, 02h01   #1
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut pow type problem

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


  Réponse avec citation
Vieux 27/05/2008, 02h10   #2
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem

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


  Réponse avec citation
Vieux 27/05/2008, 02h11   #3
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem

In article <z3J_j.3440$tF1.560@trnddc01>,
Bill Cunningham <nospam@nspam.com> wrote:
> 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.)
--
"Whenever there is a hard job to be done I assign it to a lazy
man; he is sure to find an easy way of doing it."
-- Walter Chrysler
  Réponse avec citation
Vieux 27/05/2008, 02h14   #4
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem

"Bill Cunningham" <nospam@nspam.com> writes:
> 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.

> return 0;
> }
>
> And I did try to link with libm.a


--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  Réponse avec citation
Vieux 27/05/2008, 05h18   #5
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem


"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> wrote in message
news:g1fn48$k1i$1@canopus.cc.umanitoba.ca...

> 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


  Réponse avec citation
Vieux 27/05/2008, 05h19   #6
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem


"Keith Thompson" <kst-u@mib.org> wrote in message
news:ln63t0tuou.fsf@nuthaus.mib.org...

> 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


  Réponse avec citation
Vieux 27/05/2008, 10h18   #7
Nick Keighley
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: pow type problem

On 27 May, 02:01, "Bill Cunningham" <nos...@nspam.com> wrote:

> #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;
>
> }



--
Nick Keighley

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15776 seconds with 15 queries