# Thread: how to transform integer to char..

1. ## 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. 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. 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.

4. i can do casting??

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

5. 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.

6. 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'

7. Maybe you should take a look at the itoa and atoi functions

8. 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).