C

Discussion in 'C Programming' started by neha_chhatre@yahoo.co.in, Oct 7, 2007.

  1. Guest

    how to convert integer to char
    , Oct 7, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >how to convert integer to char


    sprintf()

    --
    "Any sufficiently advanced bug is indistinguishable from a feature."
    -- Rich Kulawiec
    Walter Roberson, Oct 7, 2007
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    > how to convert integer to char


    In C there are several integer types like short, int, long and long
    long. Even char is an integer. One example for int would be.

    int i;
    char c;

    i = getc(stdin);
    if (i != EOF) {
    c = i;
    putchar(c);
    }

    The value in the int, (or any other integer), must be representable by
    the char variable. Otherwise there will be loss of data and unexpected
    behaviour.
    santosh, Oct 7, 2007
    #3
  4. santosh Guest

    wrote:

    > how to convert integer to char


    What is the idea posting the same question thrice within ten minutes?
    Usenet is an asynchronous medium and it will take a while for your
    article to propagate to all of Usenet and for any possible answer to
    reach your news server.

    Be aware that Google Groups is _not_ Usenet, but a rather fragile web
    interface to it. It might be better to avail of a dedicated Usenet
    server and a newsreader.
    santosh, Oct 7, 2007
    #4
  5. On Sun, 7 Oct 2007 17:31:02 +0000 (UTC), in comp.lang.c ,
    -cnrc.gc.ca (Walter Roberson) wrote:

    >In article <>,
    > <> wrote:
    >>how to convert integer to char

    >
    >sprintf()


    that converts it to a string, not a char.

    (though I think you're right that this is what the OP probably wants)

    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Oct 7, 2007
    #5
  6. Army1987 Guest

    On Sun, 07 Oct 2007 10:26:17 -0700, neha_chhatre wrote:

    > how to convert integer to char

    With a cast, e.g. (char)i is i converted to char.
    But, in practically all places where this makes sense and is
    useful, that is done automatically.
    Note that library functions dealing with single characters use
    int, so in putchar(65) no conversion is done nor needed (though
    you could write putchar((char)65) if you find it clearer, because
    it'll be converted back to int, but that's useless: in C char's
    are just "small" integers).

    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 7, 2007
    #6
  7. Martin Wells Guest

    On Oct 7, 6:26 pm, wrote:
    > how to convert integer to char


    Specify a radix and alphabet.

    #define RADIX 10

    char const alphabet[RADIX] =
    { '0','1','2','3','4','5','6','7','8','9' };

    char UIntToChar(unsigned const i)
    {
    assert( i < RADIX );

    return alphabet;
    }

    Martin
    Martin Wells, Oct 7, 2007
    #7
  8. Tor Rustad Guest

    wrote:
    > how to convert integer to char
    >


    How to handle int > SCHAR_MAX ???

    --
    Tor <torust [at] online [dot] no>

    C-FAQ: http://c-faq.com/
    Tor Rustad, Oct 8, 2007
    #8
  9. writes:
    > how to convert integer to char


    If you want a post a question here, pick a subject header that
    summarizes what you're asking about. "C" is frankly a lousy subject
    header, since (in theory at least) C is all we talk about here.

    You want to know how to convert an integer to a char. What exactly do
    you mean by "convert"? I can think of at least two distinct meanings
    for your question.

    Providing some examples would help. Providing some actual code, even
    if it doesn't work, would also help.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 8, 2007
    #9
  10. Rui Maciel Guest

    wrote:

    > how to convert integer to char


    My oh my. September arrived later this year.


    Rui Maciel
    Rui Maciel, Oct 8, 2007
    #10
  11. Rui Maciel Guest

    wrote:

    > how to convert integer to char


    My oh my. September arrived later this year.


    Rui Maciel
    Rui Maciel, Oct 8, 2007
    #11
  12. user923005 Guest

    On Oct 7, 10:26 am, wrote:
    > how to convert integer to char


    Just a reminder to read the FAQ before posting:

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

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

    You can obviously use sprintf() to convert long or floating-
    point numbers to strings as well (using %ld or %f).

    References: K&R1 Sec. 3.6 p. 60; K&R2 Sec. 3.6 p. 64.
    user923005, Oct 8, 2007
    #12
  13. Richard Guest

    Rui Maciel <> writes:

    > wrote:
    >
    >> how to convert integer to char

    >
    > My oh my. September arrived later this year.
    >
    >
    > Rui Maciel


    It sure did. When do you learn not to double post? :)
    Richard, Oct 8, 2007
    #13
  14. Guest

    On Oct 8, 6:43 am, Martin Wells <> wrote:
    > On Oct 7, 6:26 pm, wrote:
    >
    > > how to convert integer to char

    >
    > Specify a radix and alphabet.
    >
    > #define RADIX 10
    >
    > char const alphabet[RADIX] =
    > { '0','1','2','3','4','5','6','7','8','9' };
    >
    > char UIntToChar(unsigned const i)
    > {
    > assert( i < RADIX );
    >
    > return alphabet;
    >
    > }
    >


    or this one

    char inttochar(int i)
    {
    return i >= 0 && i <= 9 ?
    i + ('1' - 1) :
    i ;
    }
    , Oct 8, 2007
    #14
  15. On Oct 7, 10:26 pm, wrote:
    > how to convert integer to char


    1) itoa() -> converts an integer to a character array
    2) cast tricks with some data loss
    3) sprintf tricks w.r.t string
    4) a small routine on your own

    Karthik Balaguru
    karthikbalaguru, Oct 11, 2007
    #15
  16. user923005 Guest

    On Oct 8, 12:44 pm, ""
    <> wrote:
    > On Oct 8, 6:43 am, Martin Wells <> wrote:
    >
    >
    >
    >
    >
    > > On Oct 7, 6:26 pm, wrote:

    >
    > > > how to convert integer to char

    >
    > > Specify a radix and alphabet.

    >
    > > #define RADIX 10

    >
    > > char const alphabet[RADIX] =
    > > { '0','1','2','3','4','5','6','7','8','9' };

    >
    > > char UIntToChar(unsigned const i)
    > > {
    > > assert( i < RADIX );

    >
    > > return alphabet;

    >
    > > }

    >
    > or this one
    >
    > char inttochar(int i)
    > {
    > return i >= 0 && i <= 9 ?
    > i + ('1' - 1) :
    > i ;
    > }


    It does not seem to be terribly effective for integers larger than 9.
    user923005, Oct 11, 2007
    #16
  17. karthikbalaguru <> writes:
    > On Oct 7, 10:26 pm, wrote:
    >> how to convert integer to char

    >
    > 1) itoa() -> converts an integer to a character array
    > 2) cast tricks with some data loss
    > 3) sprintf tricks w.r.t string
    > 4) a small routine on your own


    itoa is non-standard.

    The question is ambiguous. There's no point in trying to answer it
    until and unless the original poster comes back and explains what he's
    actually asking. If he doesn't do so, I think we can assume he's not
    really interested in getting an answer.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Oct 11, 2007
    #17
    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.

Share This Page