Your first step to c/c++ programming :=-[PART 2]-=:

Go down

Your first step to c/c++ programming :=-[PART 2]-=:

Post by Admin on Sun Sep 20, 2009 5:48 pm

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 constant is a character written between single quotes. Here are some examples:

char letter = 'A';
char digit = '9';
char exclamation = '!';

You can use escape sequences to specify character constants too:

char newline = '\n';
char tab = '\t';
char single_quote = '\'';

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.


Posts : 121
Points : 24971
Reputation : 15
Join date : 2009-08-18
Age : 30
Location : algeria

View user profile

Back to top Go down

Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum