Computer Programming - Characters

If it was easy to work with numbers in computer programming, it would be even easier to work with characters. Characters are simple alphabets like a, b, c, d...., A, B, C, D,....., but with an exception. 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 single quotes. For example, the 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 a 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 single quotes, then they will be treated as a character literal and can be assigned to a variable of character type, so the 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 it can hold any of the 256 different values. A character data type can store any of the characters available on your keyboard including special characters like !, @, #, #, $, %, ^, &, *, (, ), _, +, {, }, etc.

Note that you can keep only a single alphabet or a single digit number inside single quotes and more than one alphabets or digits are not allowed inside single quotes. So the following statements are invalid in C programming −

char ch1 = 'ab';

char ch2 = '10';

Given below 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 the 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. When a character is preceded by a backslash (\), it is called an escape sequence and it has a special meaning to the compiler. For example, \n in the following statement is a valid character and it is called a new line character −

char ch = '\n';

Here, character n has been preceded by a backslash (\), it has special meaning which is a new line but keep in mind that backslash (\) has special meaning with a few characters only. The following statement will not convey any meaning in C programming and it will be assumed as an invalid statement −

char ch = '\1';   

The following table lists the escape sequences available in C programming language −

Escape Sequence

Description

\t

Inserts a tab in the text at this point.

\b

Inserts a backspace in the text at this point.

\n

Inserts a newline in the text at this point.

\r

Inserts a carriage return in the text at this point.

\f

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

The following example 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 the 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. Java handles character data types much in the same way as we have seen in C programming. However, Java provides additional support for character manipulation.

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 the above program is executed, it produces the following result −

ch1:  a

ch2:  1

ch3:  $

ch4:  +

Java also supports escape sequence in the same 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. We will study strings in a separate chapter. 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 the above program is executed, it produces the following result −

ch1:  a

ch2:  1

ch3:  $

ch4:  +

Python supports escape sequences in the same way as you have used them in C programming.