prev up next   top/contents search

comp.lang.c FAQ list · Question 8.6

Q: How can I get the numeric value (i.e. ASCII or other character set code) corresponding to a character, or vice versa?


A: In C, characters are represented by small integers corresponding to their values in the machine's character set. Therefore, you don't need a conversion function: if you have the character, you have its value. The following fragment:

	int c1 = 'A', c2 = 65;
	printf("%c %d %c %d\n", c1, c1, c2, c2);
prints
	A 65 A 65
on an ASCII machine.

To convert back and forth between the digit characters and the corresponding integers in the range 0-9, add or subtract the constant '0' (that is, the character value '0').

See also questions 8.9, 13.1, and 20.10.


prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North