View Full Version : Char variables

Code Wizard
05-02-2004, 05:07 PM
Variables of type char can be assigned values in these ways:

char x='a';
char x=65;

Why should I use the second form,and why are the benefits from using it?
Can I do things like:
char x=543454; ?

05-02-2004, 05:36 PM
chars are usually one byte or two so something like:

char x=543454;

will probably give you an out of bounds error or some unexpected results (depending on the language/platform you use) because the number is out of range.

But basically, as far as the computer knows, characters are the same as numbers. There are tables that do the conversion. 'a' and 65 are the same thing for the machine.

Why should you use the second form? If the variable is actually representing characters then you probably shouldn't since it would make the code less clear. But many people use chars when they know a number will fit in their range and they want to save some memory. For example, to represent a bitmap with 8-bit color depth, you'll probably want to use an array of chars.