English 中文(简体)
Programming - Characters
  • 时间:2024-12-22

Computer Programming - Characters


Previous Page Next Page  

If it was easy to work with numbers in computer programming, it would be even easier to work with characters. Characters are simple alphabets pke a, b, c, d...., A, B, C, D,....., but with an exception. In computer programming, any single digit number pke 0, 1, 2,....and special characters pke $, %, +, -.... 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 pteral or a character constant. Not only a, b, c,.... but when any number pke 1, 2, 3.... or any special character pke !, @, #, #, $,.... is kept inside single quotes, then they will be treated as a character pteral and can be assigned to a variable of character type, so the following is a vapd 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 pes 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 pke !, @, #, #, $, %, ^, &, *, (, ), _, +, {, }, 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 invapd 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>

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

   printf( "ch1: %c
", ch1);
   printf( "ch2: %c
", ch2);
   printf( "ch3: %c
", ch3);
   printf( "ch4: %c
", 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, in the following statement is a vapd character and it is called a new pne character −

char ch =  
 ;

Here, character n has been preceded by a backslash (), it has special meaning which is a new pne 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 invapd statement −

char ch =  1 ;

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

Escape Sequence Description
Inserts a tab in the text at this point.
 Inserts a backspace in the text at this point.
Inserts a newpne in the text at this point.
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>

int main() {
   char  ch1;
   char  ch2;
   char  ch3;
   char  ch4;
   
   ch1 =  	 ;      
   ch2 =  
 ;

   printf( "Test for tabspace %c and a newpne %c will start here", ch1, ch2);
}

When the above program is executed, it produces the following result −

Test for tabspace     and a newpne 
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.

pubpc class DemoJava {
   pubpc static void main(String []args) {
      char  ch1;
      char  ch2;
      char  ch3;
      char  ch4;
   
      ch1 =  a ;      
      ch2 =  1 ;
      ch3 =  $ ;
      ch4 =  + ;  

      System.out.format( "ch1: %c
", ch1);
      System.out.format( "ch2: %c
", ch2);
      System.out.format( "ch3: %c
", ch3);
      System.out.format( "ch4: %c
", 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.

Advertisements