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';
Printable View
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';
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...
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.
i can do casting??
Code:int x=3;
char y;
y=(char)x;
Yes, e.g.,
You might want to assert(x >= 0 && x <= 9) since this is only supposed to work with x being in that range.Code:int x = 3;
char y;
y = (char)(x + '0');
Yes, casts work for all elementary types (with possible side effects).Quote:
i can do casting??
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'
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).Quote:
Originally Posted by KroniskBakfylla