Library Function for converrting double to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sanchit

    Library Function for converrting double to string

    Library Function for converrting double to string in GCC.

    -Thanks
    Sanchit
  • Morris Dovey

    #2
    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

    Comment

    • Eric Sosman

      #3
      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

      Comment

      • santosh

        #4
        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.

        Comment

        • Richard Heathfield

          #5
          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

          Comment

          • Martin Ambuhl

            #6
            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


            Comment

            • Eric Sosman

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

              Comment

              Working...