how to transform integer to char..

This is a discussion on how to transform integer to char.. within the C Programming forums, part of the General Programming Boards category; how to transform integer to char ? int x=3; char y; i know that in order to do the opposite ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to transform integer to char..

    how to transform integer to char ?
    int x=3;
    char y;

    i know that in order to do the opposite thing
    i need to
    Code:
    int x;
    char y='3';
    
    x=y-'0';

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    a 'char' is an unsigned /or/ signed integer of at least 8 bits,
    an 'int' is a signed integer of at least 16 bits (to be checked) so you can cast from one to the other with the expected loss from int to char...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Well, if the opposite is to subtract, then perhaps what you should do is to add. Of course, the assumption is that the integer is only one digit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i can do casting??

    Code:
    int x=3;
    char y;
    y=(char)x;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Yes, e.g.,
    Code:
    int x = 3;
    char y;
    y = (char)(x + '0');
    You might want to assert(x >= 0 && x <= 9) since this is only supposed to work with x being in that range.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    i can do casting??
    Yes, casts work for all elementary types (with possible side effects).

    C-compiler Design Rules*:
    rule 1: you do not prevent the programmer from doing what [s]he wants
    rule 2: you DO NOT prevent the programmer from doing what [s]he wants

    *c99 rationale, introduction, 'Keep the spirit of C'
    Last edited by root4; 12-31-2008 at 10:59 AM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Maybe you should take a look at the itoa and atoi functions

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,269
    Quote Originally Posted by KroniskBakfylla
    Maybe you should take a look at the itoa and atoi functions
    atoi does not apply here and itoa is non-standard (but then there's the printf-family of functions).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 09:54 PM
  2. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 10:22 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  5. checking if a char * is an integer
    By gregulator in forum C Programming
    Replies: 5
    Last Post: 04-16-2004, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21