Sunday, 16 September 2012

Chapter 9 - The Char Data Type

In the previous chapters we have learnt 2 Data Types, int and float.

Now its time to learn how to use the char (character) Data Type.
This data type is slightly different from what we have learned so far. The knowledge of character will help u later to store and print names.
By the end of this post i guarantee you that you will know all the basics of a character data type and capable of using it in your own program.

How do we use it ?

In our first code we will simply assign a value to a character data type and the print what we have stored.

/*  
 Author : Ryan Sequeira  
 Date : 16th September 2012  
 Title : implementing char data type
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  char c = 'a';

  clrscr();

  //printing the character
  printf("The character is: %c", c);

  getch();
 }

It is evident from the code that you can't simply assign a character directly to a char variable.
  • The value must be enclosed in a ' ' (single quotes)
  • Only one character can be assigned to a char variable. So dont  try something like this c = 'name' . This will return an error. Be patient, we will deal with full words later.

More on Character

You might be knowing that computer deals with only digital data ( i.e 0's and 1's ) which are represented collectively as numbers. So how does it store a character. 

To answer this question we must know that every character has a ASCII value assigned to it. The complete ASCII table is shown below. 



So whenever you press a key on your keyboard the computer records them as these values, and not as characters.

This leads us to our next question

How do we check the ACSII value of a char ?

The code below will do just that. 

/*  
 Author : Ryan Sequeira  
 Date : 16th September 2012  
 Title : implementing char data type 
    by assigning ascii value
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  char c = 65;

  clrscr();

  //printing the character
  printf("The character is: %c", c);
  printf("\nIts ASCII value is: %d", c);

  getch();
 }

As you must have noticed you can assign an ASCII value to a character just as you assign a value to an integer.

Note : The value should be in the range of 0 - 127 , or else you will be assigning an illegal value which will lead to an error in your code.


Playing with the ASCII values

Now that you know that ascii values can be assigned like integers lets try to experiment with the logic. What i have done in the code below is incremented the ASCII value of char by 1.

/*  
 Author : Ryan Sequeira  
 Date : 16th September 2012  
 Title : implementing char data type 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  char c = 'a';

  clrscr();

  //printing the character
  printf("The character is: %c", c);
  printf("\nIts ASCII value is: %d", c);

  //increment the vale of c
  c = c + 1;
  printf("\n\nThe new value is : %c",c);
  printf("\nThe new ACSII value is: %d",c);

  getch();
 }

You can try incrementing it by 5 or 10, or multiplying it by 2. But make sure the value falls within the range of 0 - 127.


The Master code

What i have done in the code below is combined all the concepts we have learned in this tutorial so far and made a simple program. Try implementing it yourself and also make some changes to experiment with the results.

/*  
 Author : Ryan Sequeira  
 Date : 16th September 2012  
 Title : implementing char data type 
*/  

#include<stdio.h>
#include<conio.h>

void main()
 {
  char c = 'a';

  clrscr();

  //initial value of c
  printf("The value is: %c",c);
  printf("\nIts ASCII value is: %d",c);

  //assinging a value to char
  c = '%';
  printf("\n\nThe new value is: %c",c);
  printf("\nIts new ASCII value is: %d",c);

  //assigning an ASCII value to char
  c = 67;
  printf("\n\nThe latest value is: %c",c);
  printf("\nIts latest ASCII value is: %d",c);


  getch();
 }

No comments:

Post a Comment