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 > Problem with atoi and strlod
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with atoi and strlod

Réponse
 
LinkBack Outils de la discussion
Vieux 13/04/2008, 17h50   #1
Sanchit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with atoi and strlod

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

int main()
{
double ans;
ans = strtod("25", NULL);
printf("ans = %d\n",ans);

}

OUTPUT :
ans = 0

EXPECTED OUTPUT
ans = 25

Can anyone please explain this... Else tell me an alternative for
converting string to int.
  Réponse avec citation
Vieux 13/04/2008, 17h56   #2
Morris Dovey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Sanchit wrote:
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main()
> {
> double ans;
> ans = strtod("25", NULL);
> printf("ans = %d\n",ans);
>
> }
>
> OUTPUT :
> ans = 0
>
> EXPECTED OUTPUT
> ans = 25
>
> Can anyone please explain this... Else tell me an alternative for
> converting string to int.


try %f

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
  Réponse avec citation
Vieux 13/04/2008, 18h00   #3
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Sanchit <sanchitgupta.1@gmail.com> writes:

> #include<stdio.h>
> #include<stdlib.h>
>
> int main()
> {
> double ans;
> ans = strtod("25", NULL);
> printf("ans = %d\n",ans);
>
> }
>
> OUTPUT :
> ans = 0
>
> EXPECTED OUTPUT
> ans = 25
>
> Can anyone please explain this... Else tell me an alternative for
> converting string to int.


Alternatively you could look in the manual and read what is says about
the functions you are using.

Start with the manual page for strtod and then look at the manual page
for the format specifiers in printf.

Seriously, reading the man pages for the functions is an art in itself
and something you want to try.


  Réponse avec citation
Vieux 13/04/2008, 21h37   #4
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Sanchit wrote:
> #include<stdio.h>
> #include<stdlib.h>
>
> int main()
> {
> double ans;
> ans = strtod("25", NULL);
> printf("ans = %d\n",ans);
>
> }
>
> OUTPUT :
> ans = 0
>
> EXPECTED OUTPUT
> ans = 25
>
> Can anyone please explain this... Else tell me an alternative for
> converting string to int.


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

int main(void)
{
double ans_d;
int ans_i;
printf("The original poster used strtod to interpret the string\n"
"\"25\" as a float. This is OK; strtof and strtold\n"
"would do as well.\n");
ans_d = strtod("25", NULL);
printf("The original poster used \"%%d\" (specifier for an int)\n"
"trying to print a double. This has been changed\n"
"to one of the correct specifiers (\"%%g\")\n"
" ans_d = %g\n\n", ans_d);

printf("Since the desired value is an integer, one might consider\n"
"using one of strtol, strtoll, strtoul, or strtoull"
" instead.\n"
"(C99 also offers strtoimax, strtouimax, and wchar_t"
" versions\n"
"should be available as well.)\n" "Here I use strtol.\n");
ans_i = strtol("25", NULL, 10);
printf(" ans_i = %d\n\n", ans_i);

printf
("In these examples, no use is made of the endpointer that"
" these\n"
"functions return. Nor is any use made of errno.\n"
"These are valuable for any real use of these functions,\n"
"and you should learn to use them.\n");
return 0;

}



The original poster used strtod to interpret the string
"25" as a float. This is OK; strtof and strtold
would do as well.
The original poster used "%d" (specifier for an int)
trying to print a double. This has been changed
to one of the correct specifiers ("%g")
ans_d = 25

Since the desired value is an integer, one might consider
using one of strtol, strtoll, strtoul, or strtoull instead.
(C99 also offers strtoimax, strtouimax, and wchar_t versions
should be available as well.)
Here I use strtol.
ans_i = 25

In these examples, no use is made of the endpointer that these
functions return. Nor is any use made of errno.
These are valuable for any real use of these functions,
and you should learn to use them.
  Réponse avec citation
Vieux 14/04/2008, 03h17   #5
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

On Sun, 13 Apr 2008 09:50:50 -0700 (PDT), Sanchit
<sanchitgupta.1@gmail.com> wrote:

>#include<stdio.h>
>#include<stdlib.h>
>
>int main()
>{
> double ans;
> ans = strtod("25", NULL);
> printf("ans = %d\n",ans);
>
>}
>
>OUTPUT :
>ans = 0
>
>EXPECTED OUTPUT
>ans = 25
>
>Can anyone please explain this... Else tell me an alternative for
>converting string to int.


You some objection to strtol?


Remove del for email
  Réponse avec citation
Vieux 14/04/2008, 03h18   #6
Chris McDonald
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Barry Schwarz <schwarzb@dqel.com> writes:

>You some objection to strtol?


You have some objection to verbs?

--
Chris.
  Réponse avec citation
Vieux 14/04/2008, 05h16   #7
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Sanchit wrote:
>
> #include<stdio.h>

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

#include <stdlib.h>
>
> int main() {

int main(void) {
> double ans;
> ans = strtod("25", NULL);
> printf("ans = %d\n",ans);

printf("ans = %f]\n", abs);
return 0;
> }


Obvious program faults/failings marked above.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 14/04/2008, 06h32   #8
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Chris McDonald said:

> Barry Schwarz <schwarzb@dqel.com> writes:
>
>>You some objection to strtol?

>
> You have some objection to verbs?


Do you have some objection to properly-formed verbs?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
  Réponse avec citation
Vieux 14/04/2008, 06h49   #9
Peter Nilsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

CBFalconer wrote:
> Sanchit wrote:
> > #include<stdio.h>

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

>
> #include <stdlib.h>
>
> > int main() {

>
> int main(void) {
>
> > double ans;
> > ans = strtod("25", NULL);
> > printf("ans = %d\n",ans);

>
> printf("ans = %f]\n", abs);


ITYM: printf("ans = %f\n", ans);

> return 0;
> > }

>
> Obvious program faults/failings marked above.


Define faults/failings. Only one of your replacements pertains
to an actual error. The other code items are style issues.
[Not that I disagree with your suggested additions.]

--
Peter
  Réponse avec citation
Vieux 14/04/2008, 17h25   #10
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with atoi and strlod

Richard Heathfield <rjh@see.sig.invalid> writes:
> Chris McDonald said:
>
>> Barry Schwarz <schwarzb@dqel.com> writes:
>>
>>>You some objection to strtol?

>>
>> You have some objection to verbs?

>
> Do you have some objection to properly-formed verbs?


Objection sentence fragments?

--
Keith Thompson (The_Other_Keith) <kst-u@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
  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 23h20.


É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,14806 seconds with 18 queries