Sei sulla pagina 1di 5

Lesson 6: ASCII code and character variables - C Tutorial This lesson, as can be presumed from its title, will

teach you what is ASCII code, how does it affect programmers variables and what is the difference between number and numerical figure. This ones easy... Hope you still havent pulled back; just when I hit you in the face with hardcore code in next few lessons, dont say I caught you off guard :) Developing your own program, even as simple as calculator can give you nice, productive feeling.

DISPLAY OF SYMBOLS IN PROGRAM LANGUAGE C a) Archiving small Integer numbers b) Archiving letters, punctuation signs, special symbols Character type variable (char) is capable of saving alphanumeric data, and it uses 1 octet (byte) for doing it. Alphanumeric symbols are: letters, numbers and special symbols that can be input via keyboard. Character type variables can have for-sign (signed char) or have no for-sign (unsigned char). Declaration in program language C, and numerical range that is covered by following data types: char [-128, 127] unsigned char [0,255]

Universal standard: 7-bit ASCII code (American Standard Code for Information Interchange) In program language C, all the characters are saved as a number sequence that represent ASCII value (code) of the preferred character Important ASCII values:

0 character NULL (\0) 32 space ( ) 48 57 numbers 0-9 65 90 capital letters A to Z 97 122 small letters a to z (97 65 = 32 difference between capital and small letter!)

CHARACTER CONSTANTS Character constants are given inside ( ) singular quotation marks, NOT ( ):

char a; a = X; Example: Variable c is character type. Value of letter A is assigned to it in a few different ways: c = A; c = 65; // ASCII code of letter A is //6510 = 4116 = 1018 c=\x41; // hexadecimal constants begin //with \x c=\0101; // octal constants begin with s \0

Example: Variable c is character type. Use it to assign values of single quotation mark () and character \ c=\; // special characters inside // single quotation mark must have prefix \ c=\\; When character type is used to save number figures, we must be careful and pay attention that char variable saves ASCII value of that particular figure, not the actual value, in short: char a; a = 1; // equal to: a = 49; Variable a contains numerical value 49, what is ASCII value of character 1. However if we want to get numerical value of a character, then we take out 48 from value that is saved. 48 presents ASCII figure. Value 48 presents ASCII figure 0. Its important to notice that some numerical figures presented as ASCII characters, dont represent binary expressions of the same figures shown as integers.

Example: Variable a, (type char) is holding figure 7. Transform this value to a number!

Binary expression of variable a is 0011 0111 (ASCII value 5510). char a = 7; short int number; number = a 48; or number = a 0; or number = a & 0x0f; 0011 0111 (5510) 0000 1111 (0x0f) --------0000 0111 (710)

Example: char c =A; Whats the result of following executions? printf printf printf printf ("%c", ("%d", ("%c", ("%d", c); c); c + B // result A // result 65 32); // result a A)); // result 1

Resulting symbols are displayed on your monitor (printf).

Example: Given variables a and b (type char) consist of figures (0-9). Write down an expression that will calculate number that is equal to sum of these numerical figures (for example, result of 5 and 6 should be 11.) char a,b; int i; i = a - 0 + b - 0; or i = a + b 2 * 0; or i = a + b - 96; // 2 * 48 Technorati Tags: C++, Visual C, Value, Asigning, Variable, Character, ASCII

7 Responses to Lesson 6: ASCII code and character variables sabarinathan on 5:21 AM THis very usefull Pavel on 11:11 AM Lovely examples all the way. Just the language and exposition (formatting) are not polished, but I guess you disregard those minor details. A+ vurdlak on 2:36 PM thnks for encouragement. I really put effort in writing it, but blogger publishing applet givs me hedache with text formatting... If someone knows applications which can help me cuting tekst from word, and transform it so that it suits blogger news publishing. thnks again Spechal on 1:11 PM You might want to try the FCK Editor for transferring data. You can copy and paste the HTML from MS Word. vurdlak on 10:34 PM Thanks Spechal but I already got MS word addon to directly publish .doc files. Either way gonna try FCK Editor. dwks on 12:38 AM > char [-128, 127] Actually, a char can be signed or unsigned, depending on the platform/compiler flags. You should put this instead: signed char [-128, 127] char [signed char or unsigned char] me on 2:17 PM Actually, a char does not even have to have 8 bit in C, nor does the system need to use ASCII at all.Posting your comment... Leave a Reply Name (required) Website Convert to boldConvert to italicConvert to link Remember me

This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.

Potrebbero piacerti anche