+ 1

How to convert int type to char type

20th Jul 2017, 4:55 PM
Vivek
Vivek - avatar
3 Réponses
+ 11
You can convert an int type simply by assigning to a char. int c = 97; char a = c;
20th Jul 2017, 5:05 PM
Dev
Dev - avatar
0
I think that's an implicit cast... char b = 'B'; int i = b; ... should compile with a moan if -Wall was passed to the compiler. OTOH... char b = 'B'; int x = (int) b; int y = int(b); ... Will definitely work fine since it's explicitly cast. Note: the "char" type is an 8-bit int. Thus you can get away with implicit casting with only compiler warnings. I recommend explicit casting (second example) nonetheless because it makes code more readable.
20th Jul 2017, 9:46 PM
Jamie
Jamie - avatar