Lesson 6: ASCII code and character variables


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



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


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'.

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.



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';


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).




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:
, , , , , ,



7 Responses to “Lesson 6: ASCII code and character variables”

  1. Anonymous Anonymous 

    THis very usefull

  2. Anonymous Anonymous 

    Lovely examples all the way. Just the language and exposition (formatting) are not polished, but I guess you disregard those minor details.

    A+

  3. Anonymous Anonymous 

    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

  4. Anonymous Anonymous 

    You might want to try the FCK Editor for transferring data. You can copy and paste the HTML from MS Word.

  5. Anonymous Anonymous 

    Thanks Spechal but I already got MS word addon to directly publish .doc files. Either way gonna try FCK Editor.

  6. Anonymous Anonymous 

    > 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]

  7. Anonymous Anonymous 

    Actually, a char does not even have to have 8 bit in C, nor does the system need to use ASCII at all.

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


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


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




Daily Lessons for programming in Visual Studio, using C code.