Saturday 12 April 2014

Programming - Characters

It was easy to learn about numbers in computer because we are playing with numbers from childhood. Numbers are simple 1, 2, 3.....10.20, 300.345, etc.
It's even further easy to learn about characters in computer programming because you are playing with characters even before you started playing with numbers. Yes these are simple alphabets like a, b, c, d....A, B, C, D, .....but with an exception that in computer programming any single digit number like 0, 1, 2,....and special characters like $, %, +, -.... etc., are also treated as characters and to assign them in a character type variable you simply need to put them inside a single quotes. For example, following statement defines a character type variable ch and we assign a value 'a' to it:
char ch = 'a';
Here, ch is a variable of character type which can hold a character of the implementation's character set and 'a' is called character literal or a character constant. Not only a, b, c,....but when any number like 1, 2, 3.... or any special character like !, @, #, #, $,.... is kept inside a single quotes, then they will be treated as a character literal and can be assigned to a variable of character type, so following is a valid statement:
char ch = '1';
A character data type consumes 8 bits of memory which means you can store anything in a character whose ASCII value lies in between -127 to 127, so in total it can hold one of 256 different values. Bottom-line is that a character data type can store any of the characters available on your keyboard including special characters like !, @, #, #, $, %, ^, &, *, (, ), _, +, {, }, etc.
It's worth to explain it little further that you can keep only a single alphabet or single digit number inside single quotes and more than one alphabets or digits are not allowed inside single quotes. So following statements are invalid in C programming:
char ch1 = 'ab';
char ch2 = '10';
Following is a simple example, which shows how to define, assign and print characters in C Programming language:
#include <stdio.h>

main()
{
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;
   
   ch1 = 'a';      
   ch2 = '1';
   ch3 = '$';
   ch4 = '+';  

   printf( "ch1: %c\n", ch1);
   printf( "ch2: %c\n", ch2);
   printf( "ch3: %c\n", ch3);
   printf( "ch4: %c\n", ch4);
}
Here, we used %c to print a character data type. When above program is executed, it produces the following result:
ch1: a
ch2: 1
ch3: $
ch4: +

Escape Sequences:

Many programming languages support a concept called Escape Sequence. So when a character is preceded by a backslash (\), then it is called an escape sequence and has special meaning to the compiler. For example, following is a valid character and it is called new line character:
char ch = '\n';
Here, character n has been preceded by a backslash (\), so now it has special meaning which is a new line but keep in mind that backslash (\) has special meaning with few characters only, so following will not have any meaning in C programming and it will be assumed as an invalid statement:
char ch = '\1';
Following table shows correct escape sequences available in C programming language:
Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a newline in the text at this point.
\rInserts a carriage return in the text at this point.
\fInserts a form feed in the text at this point.
\'Inserts a single quote character in the text at this point.
\"Inserts a double quote character in the text at this point.
\\Inserts a backslash character in the text at this point.
Following is a simple example which shows how the compiler interprets an escape sequence in a print statement:
#include <stdio.h>

main()
{
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;
   
   ch1 = '\t';      
   ch2 = '\n';


   printf( "Test for tabspace %c and a newline %c will start here", ch1, ch2);
}
When above program is executed, it produces the following result:
Test for tabspace     and a newline 
 will start here

Characters in Java

Following is the equivalent program written in Java programming language. Java programming language handles character data type in similar way as we have seen in C programming language. Though Java provides additional support for character manipulation which you will to know when you will drill down this programming language.
You can try to execute the following program to see the output, which must be identical to the result generated by the above C example.
public class DemoJava
{    
   public static void main(String []args) 
   {

      char  ch1;
      char  ch2;
      char  ch3;
      char  ch4;
   
      ch1 = 'a';      
      ch2 = '1';
      ch3 = '$';
      ch4 = '+';  

      System.out.format( "ch1: %c\n", ch1);
      System.out.format( "ch2: %c\n", ch2);
      System.out.format( "ch3: %c\n", ch3);
      System.out.format( "ch4: %c\n", ch4);
    
   }
}
When above program is executed, it produces the following result:
ch1:  a
ch2:  1
ch3:  $
ch4:  +
Java also supports escape sequence in very similar way you have used them in C programming.

Characters in Python

Python does not support any character data type but all the characters are treated as string, which is a sequence of characters and we will study strings in a separate chapter. But you do not need to have any special arrangement while using a single character in Python.
Following is the equivalent program written in Python:
ch1 = 'a';      
ch2 = '1';
ch3 = '$';
ch4 = '+'; 

print "ch1: ", ch1
print "ch2: ", ch2
print "ch3: ", ch3
print "ch4: ", ch4
When above program is executed, it produces the following result:
ch1:  a
ch2:  1
ch3:  $
ch4:  +
Python also supports escape sequence in very similar way you have used them in C programming.

No comments:

Post a Comment