Results 1 to 3 of 3

Thread: [Resolved] int to char?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Resolved [Resolved] int to char?

    In C, how can I convert an int to a char, such as convert the int 5 to the char '5'?
    Last edited by The Hobo; Oct 9th, 2005 at 02:04 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: int to char?

    The itoa function will convert an integer into a string.

    Of course, if you just have one digit like in your example, you could just do something like:

    Code:
    int i = 5;
    char c = '0' + i; // c is '5'
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: int to char?

    don't use itoa, it is not a standard C function, on the compilers that support it it may have different behaviour!
    The official C way of converting a integer to a string is to use sprintf.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width