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';
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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');
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes, casts work for all elementary types (with possible side effects).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'
Last edited by root4; 12-31-2008 at 10:59 AM.
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).Originally Posted by KroniskBakfylla
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)