sprintf confusion

Discussion in 'C Programming' started by Bart, May 15, 2008.

  1. Bart

    Bart Guest

    On May 15, 6:21 pm, arnuld <> wrote:
    > For my program , I want to convert an integer into array of characters. I
    > see C has an atoi  function o convert from an array of characters to
    > integers but no standard function to convert from integer to an array opf
    > chars. Why so ?  Does that conversion not come into practice ?
    >
    > we have sprintf but that riddles with the buffer-overflow problem :( and
    > snprintf is not in ANSI but in C99.


    I have itoa() and ltoa() on my C system. But I suspect these may not
    be standard [why not?].

    Then sprintf seems a good alternative. Just give it a big enough
    buffer size (certainly more than 2 characters!). After all how big can
    an integer be? Give it 100 characters and be done with it.

    Or use it to create your own itoa().

    --
    Bartc
    Bart, May 15, 2008
    #1
    1. Advertising

  2. Bart

    arnuld Guest

    For my program , I want to convert an integer into array of characters. I
    see C has an atoi function o convert from an array of characters to
    integers but no standard function to convert from integer to an array opf
    chars. Why so ? Does that conversion not come into practice ?


    we have sprintf but that riddles with the buffer-overflow problem :( and
    snprintf is not in ANSI but in C99.


    I have a network program in C ( which is not topical here, hence I did
    not post the code). The client sends a number to the server and server
    squares the number and sends it back to the client. Now any communication
    between them happens using characters. So client sends an array of
    characters to the server:

    arr[] = { '3', '\0' };


    and the server reads and square the number and sends this array back to
    the client:

    arr_new[] = { '9', '\0' };


    Right now I solved the problem using sprintf and by using the number whose
    squares are of one digit and I have also limited the size of array to 2
    only.

    but is this the C way of doing things ?




    --
    http://lispmachine.wordpress.com/
    my email ID is @ the above blog.
    just check the "About Myself" page :)
    arnuld, May 15, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,593
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,059
  3. CJ
    Replies:
    1
    Views:
    1,322
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,837
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    544
    Pilatus
    Dec 18, 2003
Loading...

Share This Page