If you're a beginning programmer and want to get more deeply into programming with variables, you've come to the right place. This article, the third of three parts, is excerpted from chapter two of the book Beginning C, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
First Steps in (C) Programming, conclusion - More Numeric Data Types (Page 2 of 9 )
To complete the set of numeric data types, Iíll now cover those that I havenít yet discussed. The first is one that I mentioned previously: type char. A variable of type char can store the code for a single character. Because it stores a character code, which is an integer, itís considered to be an integer type. Because itís an integer type, the value stored can be treated just as an integer and can participate in arithmetic calculations.
The Character type
Values of typecharoccupy the least amount of memory of all the data types. They require just 1 byte. The integer thatís stored in a variable of typecharcan be interpreted as a signed or unsigned value, depending on your compiler. An unsigned integer type is simply one that allows negative as well as positive integral values to be stored. As an unsigned type, the value stored in a variable of typechar can range from 0 to 255. As a signed type, a variable of typecharcan store values from Ė128 to +127. Of course, both ranges correspond to the same set of bit patterns: from 0000 0000 to 1111 1111. With unsigned values, all 8 bits are data bits, so 0000 0000 corresponds to 0 and 1111 1111 corresponds to 255. With unsigned values, the leftmost bit is a sign bit, so Ė128 is the binary value 1000 0000, 0 is 0000 0000, and 127 is 0111 1111. The value 1111 1111 as a signed binary value is the decimal value Ė1.
Thus, from the point of view of representing character codes, which are bit patterns, it doesnít matter whether typecharis regarded as signed or unsigned. Where it does matter is when you perform arithmetic operations on values of typechar.
Acharvariable can hold any single character, so you can specify the initial value for a variable of typecharby a character constant. A character constantis a character written between single quotes. Here are some examples:
Of course, in every case the variable will be set to the code for the character between single quotes. The actual code value will depend on your computer environment, but by far the most common is American Standard Code for Information Interchange (ASCII). You can find the ASCII character set in Appendix B.
You can also initialize a variable of typecharwith an integer value, as long as the value fits into the range for typecharwith your compiler, for example:
char character = 74; /* ASCII code for the letter J */
A variable of typecharhas a sort of dual personality: you can interpret it as a character or as an integer. Hereís an example of an arithmetic operation with a value of typechar:
char letter = 'C'; /* letter contains the decimal code value 67 */ letter = letter +3; /* letter now contains 70, which is 'F' */
Thus, you can perform arithmetic on a value of typecharand still treat it as a character.
Character Input and Character Output
You can read a single character from the keyboard and store it in a variable of typecharusing thescanf()function with the format specifier%c:
char ch = 0; scanf("%c", &ch); /* Read one character */
To write a single character to the command line with theprintf()function, you use the same format specifier,%c:
printf("The character is %c", ch);
Of course, you can output the numeric value of a character too:
printf("The character is %c and the code value is %d", ch, ch);
This statement will outputchas a character and as a numeric value.