468,315 Members | 1,408 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,315 developers. It's quick & easy.

Library Function for converrting double to string

Library Function for converrting double to string in GCC.

-Thanks
Sanchit
Jun 27 '08 #1
6 1395
Sanchit wrote:
>
Library Function for converrting double to string in GCC.
sprintf()

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Jun 27 '08 #2
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
es*****@ieee-dot-org.invalid
Jun 27 '08 #3
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.

Jun 27 '08 #4
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
Jun 27 '08 #5
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
Jun 27 '08 #6
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
es*****@ieee-dot-org.invalid
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Johno | last post: by
87 posts views Thread by Robert Seacord | last post: by
20 posts views Thread by Frank-O | last post: by
4 posts views Thread by Dave | last post: by
3 posts views Thread by jchimanzi | last post: by
4 posts views Thread by jchimanzi | last post: by
reply views Thread by daylond | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.