# how to transform integer to char..

• 12-31-2008
transgalactic2
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';```
• 12-31-2008
root4
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...
• 12-31-2008
laserlight
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.
• 12-31-2008
transgalactic2
i can do casting??

Code:

```int x=3; char y; y=(char)x;```
• 12-31-2008
laserlight
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.
• 12-31-2008
root4
Quote:

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'
• 01-09-2009
KroniskBakfylla
Maybe you should take a look at the itoa and atoi functions ;)
• 01-09-2009
laserlight
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).