Lesson 6: ASCII code and character variables
Published Wednesday, February 15, 2006 by Vurdlak | E-mail this post
This lesson, as can be presumed from its title, will teach you what is ASCII code, how does it affect programmer’s variables and what is the difference between number and numerical figure. This one's easy... Hope you still haven't pulled back; just when I hit you in the face with hardcore code in next few lessons, don't 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 are given inside (' ') singular quotation marks, NOT (“ “):
a = 'X';
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
c='\0101'; // octal constants begin with s \0
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 \
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:
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'.
It's important to notice that some numerical figures presented as ASCII characters, don't represent binary expressions of the same figures shown as integers.
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)
char c ='A';
What’s the result of following executions?
printf ("%c", c); // result A
printf ("%d", c); // result 65
printf ("%c", c + 32); // result a
printf ("%d", 'B' – 'A')); // result 1
Resulting symbols are displayed on your monitor (printf).
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.)
i = a - '0' + b - '0';
i = a + b – 2 * '0';
i = a + b - 96; // 2 * 48
Technorati Tags: C++, Visual C, Value, Asigning, Variable, Character, ASCII
Daily Lessons for programming in Visual Studio, using C code.