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 > Library Function for converrting double to string
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Library Function for converrting double to string

Réponse
 
LinkBack Outils de la discussion
Vieux 13/04/2008, 14h45   #1
Sanchit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Library Function for converrting double to string

Library Function for converrting double to string in GCC.

-Thanks
Sanchit
  Réponse avec citation
Vieux 13/04/2008, 15h11   #2
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

Sanchit wrote:
> Library Function for converrting double to string in GCC.


<<BZZZT!>>

Alex Trebek: Yes, Eric?

Eric: What is the topic of Question 13.1 in the comp.lang.c
Frequently Asked Questions (FAQ) list at <http://www.c-faq.com/>?

Alex Trebek: That's right.

Eric: I'll take Undefined Behavior for 600.

--
Eric Sosman
esosman@ieee-dot-org.invalid
  Réponse avec citation
Vieux 13/04/2008, 15h51   #3
Morris Dovey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

Sanchit wrote:
>
> Library Function for converrting double to string in GCC.


sprintf()

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
  Réponse avec citation
Vieux 13/04/2008, 16h46   #4
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

Sanchit wrote:

> Library Function for converrting double to string in GCC.


sprintf and snprintf are both standardised and will both do the job,
though the latter will require C99 support and hence is less portable
than the former, but it avoids the buffer overflow problem of sprintf.

  Réponse avec citation
Vieux 13/04/2008, 21h01   #5
Richard Heathfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

santosh said:

> Sanchit wrote:
>
>> Library Function for converrting double to string in GCC.

>
> sprintf and snprintf are both standardised and will both do the job,
> though the latter will require C99 support and hence is less portable
> than the former, but it avoids the buffer overflow problem of sprintf.


sprintf doesn't *have* a buffer overflow problem, when used properly.

--
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 13/04/2008, 21h08   #6
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

Sanchit wrote:
> Library Function for converrting double to string in GCC.


Look up sprintf() and snprintf().

Those should be in any elementary C textbook.

In addition to checking your textbook before posting, it is a good idea
to check the C language FAQ <http://c-faq.com/>. Steve Summit put a lot
of effort into producing it, and many people get upset if posters don't
expend the tiny bit of effort it takes to consult it.

In this case, you might start with question 13.1
<http://c-faq.com/lib/itoa.html>. Everything that follows is from that
question and answer, but note the crossreferences to questions 7.5a,
8.6, 12.21, and 20.10, which you will need to check for yourself:

comp.lang.c FAQ list · Question 13.1

Q: How can I convert numbers to strings (the opposite of atoi)? Is there
an itoa function?

A: Just use sprintf:

sprintf(string, "%d", number);

(Don't worry that sprintf may be overkill, potentially wasting run time
or code space; it works well in practice.) See also the examples in the
answer to question 7.5a, and also questions 8.6 and 12.21.

You can obviously use sprintf to convert long or floating-point numbers
to strings as well (using %ld or %f); in other words, sprintf can also
be thought of as the opposite of atol and atof. In addition, you have
quite a bit of control over the formatting. (It's for these reasons that
C supplies sprintf as a general solution, and not itoa.)

If you simply must write an itoa function, here are some things to consider:

* There is a sample implementation in K&R.
* You'll have to worry about return buffer allocation; see question
7.5a.
* A naïve implementation usually doesn't handle the most-negative
integer (INT_MIN, usually -32,768 or -2,147,483,648) properly.

See also questions 12.21 and 20.10.

References: K&R1 Sec. 3.6 p. 60
K&R2 Sec. 3.6 p. 64


  Réponse avec citation
Vieux 13/04/2008, 21h40   #7
Eric Sosman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Library Function for converrting double to string

Richard Heathfield wrote:
> santosh said:
>
>> Sanchit wrote:
>>
>>> Library Function for converrting double to string in GCC.

>> sprintf and snprintf are both standardised and will both do the job,
>> though the latter will require C99 support and hence is less portable
>> than the former, but it avoids the buffer overflow problem of sprintf.

>
> sprintf doesn't *have* a buffer overflow problem, when used properly.


Deciding in advance how much output an "%f" conversion
will generate can be a tricky business. The best I can
think of is to use an output buffer whose size is the sum of

1 for a possible minus sign,
DBL_MAX_10_EXP integer digits (note no `-1'),
1 for the decimal point (if not suppressed),
N fraction digits (in "%.Nf", 6 if not specified),
1 for the trailing '\0', and
2 in case I and/or sprintf() make off-by-one errors.

Like Ubik, sprintf() is "safe when taken as directed."

--
Eric Sosman
esosman@ieee-dot-org.invalid
  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 02h55.


É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,15093 seconds with 15 queries